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
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
Write a program factor that prints the prime factors of a specified non-negative integer. The integer...
Write a program factor that prints the prime factors of a specified non-negative integer. The integer will be provided as a command-line argument. You may assume that the integer will be greater than 1 and less than or equal to the largest signed integer on your platform (that is, you may use an int).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT