Question

In: Computer Science

The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...

The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series shown in Eq. 1 below. Since we cannot actually approach infinity, we can assume a function fk to take the form shown in Eq. 2, and say that fk is related to e through Eq. 3. Eq. 1 Eq. 2 Eq. 3 e = ∑ 1 n! n→∞ n=0 fk = ∑ 1 n! n=k n=0 lim k→∞ fk = e Examples are provided below. We can see that when k is increased, the value of fk converges to a specific value (i.e. Euler's number) and the error decreases. The error here is defined as error = | approx−true true |, where approx refers to the approximation of Euler's number according to Eq. 2 above, and true value is taken to be MATLAB's Euler's number. f0 = 1 0! = 1 error = 0.6321 f1 = 1 0! + 1 1! = 2 error = 0.2642 f2 = 1 0! + 1 1! + 1 2! = 2.5 error = 0.0803 f3 = 1 0! + 1 1! + 1 2! + 1 3! = 2.6667 error = 0.0190 ... B. Determine the value of k in which the value of fk produces an error of less than 1e-10.

Solutions

Expert Solution

%Function for part a factorial_n.m

function nFact = factorial_n(n)
if n<0 || (floor(n)-n)~=0
    error('Please enter a positive integer!')
else
    nFact=1;
    for i=1:n
        nFact=nFact*i;
    end
  
end

%script for part B

approx=1;
k=1;
while abs(approx-exp(1))>=1e-10
    approx = approx + 1/factorial_n(k);
    k=k+1;
end
fprintf('Value of k for which error is less than 1e-10:%d\n',k-1)
fprintf('Error: %.6e\n',abs(approx-exp(1)))
fprintf('Calculated value of fk: %.10f\n',approx)


   


Related Solutions

In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
The factorial of a natural number n is denoted by n! and is defined as follows:...
The factorial of a natural number n is denoted by n! and is defined as follows: the factorial of 0 is 1, the factorial of 1 is 1, and the factorial of any other positive integer is the product of all the integers from 2 to that integer. Write a VBA function called MyFactorial with one input of data type Integer which computes and returns the value of the factorial of the input if it is greater than or equal...
prove or disprove .if n is a non negative integer, then 5 divides 2 ⋅ 4^n...
prove or disprove .if n is a non negative integer, then 5 divides 2 ⋅ 4^n + 3⋅9^n.
The factorial of an integer ? is defined using the following equation: ?! = ? ∗...
The factorial of an integer ? is defined using the following equation: ?! = ? ∗ (? − 1) ∗ (? − 2) ∗ … ∗ 2 ∗ 1 In combinatorics, permutations is the number of ways of selecting a subset of some elements such that the order matters. For example, there are 6 permutations of letters A, B, and C. A B C ,A C B, B A C, B C A, C A B, C B A, The...
Use a direct proof to prove that 6 divides (n^3)-n whenever n is a non-negative integer.
Use a direct proof to prove that 6 divides (n^3)-n whenever n is a non-negative integer.
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
Which codes add 1 to integer n? 1) n=n+1; 2) n++; 3)++n; 4) n+=1; 5) n=--n+2
Which codes add 1 to integer n? 1) n=n+1; 2) n++; 3)++n; 4) n+=1; 5) n=--n+2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT