The factorial of a nonnegative integer n, written as n!, is a mathematical calcu
ID: 3665400 • Letter: T
Question
The factorial of a nonnegative integer n, written as n!, is a mathematical calculation that is the product of all positive integers less than or equal to n. The calculation of factorials is one of the example of the use of recursion. The calculation can be written as
n! = n * (n-1) * (n-2) * … 1
where 1! = 1, and 0! is defined to be 1 as well.
In this exercise, you will be using factorials to calculate a mathematical series. Perform the following steps for this exercise:One of the tenets of modern Software Engineering is code reuse. There are many instances of functions that implement the factorial schema on the Internet. Locate a recursive instance , code it and test it.Use the factorial function to calculate the series:
e = 1/0! + 1/1! + 1/2! + 1/3! + … for 50 terms.
Write the same series calculation using an iterative loop of your choice.
Instrument your code to measure time for the recursive and iterative loop methods to calculate the first 50 terms of the series.
Present your timing results.
Based on your timing results, comment on the quote: ”Would any of you very experienced programmers, if asked to code an iterative loop (like a mathematical series), think of using recursion or would that be a ‘mistake’ that a rookie programmer would leap to?”
Explanation / Answer
package cgegg;
import java.security.Timestamp;
public class Factorial {
static long factorialRecursive(int n) {
if(n == 1 || n==0) // base case
return 1;
return n* factorialRecursive(n-1); // recursive case
}
public static void main(String[] args) {
// using recursion factorial method to calculate expression
double result = 0;
long startTime = System.currentTimeMillis();
for(int i =1; i<=50; i++) {
result = result + (1.0/factorialRecursive(i));
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Time taken by recursive methos: "+elapsedTime);
System.out.println("Value of expressio: "+result);
//using iterative method to calculate expression
long currentFact = 1;
double result2 = 0;
long startTime1 = System.currentTimeMillis();
for(int i=1; i<=50; i++) {
currentFact = currentFact * i;
result2 = result2 + (1.0/currentFact);
}
long stopTime1 = System.currentTimeMillis();
long elapsedTime1 = stopTime1 - startTime1;
System.out.println("Time taken by iterative methos: "+elapsedTime1);
System.out.println("Value of expressio: "+result2);
}
}
Output:
Time taken by recursive methos: 1
Value of expressio: 1.7182818284590455
Time taken by iterative methos: 0
Value of expressio: 1.7182818284590455
/* I will always use iterative where ever it is possible, because in recursive it uses extra internal space
* Also in recursive we are not using previously calculated value, thats why it is taking more time than iterative.
*
* In terative 'currentFact' variable is used to store the previously calculated value that is used in next iteration
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.