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...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2* 1 = 120 3! = 3 * 2 * 1 = 6 2! = 2 * 1 = 2 The value of 0! is 1. Write a program that asks user to enter an integer > 0; if a valid value is...
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 nonnegative integer n is written n! (Pronounced “n factorial”) and is defined...
The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined as follows: n! = n.(n-1). (n-2).……...1 (for values of n greater than 1) n!=1 (for n = 0 or 1) For instance, 5! = 5.4.3.2.1 which is 120. Write a Python program that reads a nonnegative integer n and calculates and prints its factorial. Your program should display a suitable error message if n entered as a negative integer.    Figure   6.   Exercise   6  ...
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.
In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of...
In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of non-negative numbers from 1 to n. Design a program that asks the user to enter a nonnegative integer and then displays the factorial of that number. Module main. Asks the user to enter a non-negative integer. A loop is used to require user input until a nonnegative number is entered. Once a nonnegative number is entered, the integer is...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT