The factorial of a nonnegative integer n, written as n!, is a mathematical calcu
ID: 3665851 • 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: 1. 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. 2. Use the factorial function to calculate the series: e = 1/0! + 1/1! + 1/2! + 1/3! + … for 50 terms. 3. Write the same series calculation using an iterative loop of your choice. 4. Instrument your code to measure time for the recursive and iterative loop methods to calculate the first 50 terms of the series. 5. Present your timing results. 6. 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
1)
import java.util.Scanner;
public class Recursive {
public static void main(String[] args) {
int n = 10;
System.out.println("Please enter n value: " );
Scanner sc= new Scanner(System.in);
n= sc.nextInt();
CalculateFactorial cf= new CalculateFactorial();
long f=cf.factorials(n);
System.out.println("Factorial Count is: " + f);
sc.close();
}
}
class CalculateFactorial
{
long factorials(int i)
{
if(i==0 || i==1)
return 1;
else
return i*factorials(i-1);
}
}
----------------------------------------------------------------
2) Recursive
import java.util.Scanner;
public class Recursive {
public static void main(String[] args) {
int n = 10;
System.out.println("Please enter n value: " );
Scanner sc= new Scanner(System.in);
n= sc.nextInt();
CalculateFactorial cf= new CalculateFactorial();
double f=cf.FactorialSum(n);
System.out.println("Series sum is: " + f);
sc.close();
}
}
class CalculateFactorial
{
long factorials(int i)
{
if(i==0 || i==1)
return 1;
else
return i*factorials(i-1);
}
double FactorialSum(int n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
sum += 1.0/factorials(i);
return sum;
}
}
------------------------------------------------------------------
3) iterative
import java.util.Scanner;
public class NonRecursive {
public static void main(String[] args) {
int n = 10;
System.out.println("Please enter n value: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
CalculateFactorial cf = new CalculateFactorial();
double f = cf.FactorialSum(n);
System.out.println("Factorial series sum is: " + f);
sc.close();
}
}
class CalculateFactorial {
// A Simple Function to return value of 1/1! + 1/2! + .. + 1/n!
double FactorialSum(int n) {
double sum = 0;
for (int i = 0; i <= n; i++)
sum += 1.0 / factorial(i);
return sum;
}
// iterative
double factorial(int n) {
int res = 1;
if (n == 0 || n == 1)
return 1;
for (int i = 1; i <= n; i++)
res = res * i;
return res;
}
}
-----------------------------------------------------
4)
import java.util.Calendar;
import java.util.Scanner;
public class NonRecursive {
public static void main(String[] args) {
int n = 10;
System.out.println("Please enter n value: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
CalculateFactorial cf = new CalculateFactorial();
long millisStartRec = Calendar.getInstance().getTimeInMillis();
//recursive
double rf = cf.FactorialSuminRecursive(n);
long recTime = Calendar.getInstance().getTimeInMillis()-millisStartRec;
//non-recursive
long millisStartNonRec = Calendar.getInstance().getTimeInMillis();
double nrf = cf.FactorialSuminNonRecursive(n);
long nonRecTime = Calendar.getInstance().getTimeInMillis()-millisStartNonRec;
System.out.println("Factorial series sum & time taken - Recursive: " + rf +" & " + recTime);
System.out.println("Factorial series sum & time taken - iterative: " + nrf+" & " + nonRecTime);
sc.close();
}
}
class CalculateFactorial {
//recursive
double factorialRecursive(int i) {
if (i == 0 || i == 1)
return 1;
else
return i * factorialRecursive(i - 1);
}
// A Simple Function to return value of 1/1! + 1/2! + .. + 1/n! in -recursive
double FactorialSuminRecursive(int n) {
double sum = 0;
for (int i = 0; i <= n; i++)
sum += 1.0 / factorialRecursive(i);
return sum;
}
// A Simple Function to return value of 1/1! + 1/2! + .. + 1/n! in ( non recursive)
double FactorialSuminNonRecursive(int n) {
double sum = 0;
for (int i = 0; i <= n; i++)
sum += 1.0 / factorial(i);
return sum;
}
// iterative
double factorial(int n) {
int res = 1;
if (n == 0 )
return 1;
for (int i = 1; i <= n; i++)
res = res * i;
return res;
}
}
5 and 6 th Comments:
Output
--------
Please enter n value:
50
Factorial series sum & time taken in milliseconds- Recursive: 2.7182818284590455 & 5
Factorial series sum & time taken in milliseconds- iterative: Infinity & 0
if n value 20:
Please enter n value:
20
Factorial series sum & time taken - Recursive: 2.7182818284590455 & 4
Factorial series sum & time taken - iterative: 2.718281834649448 & 0
----------------------------------------------------------------------------------------
From the above two results: recursive is more effective than iterative. Because it is giving more effective result than iterative. We are able to see the result than performance. This is my openion by observing this result
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.