public class Factorial { /*Class Factorial computes the factorial function both iteratively, and using recursion. */ public Factorial() { /*Parameters NONE Initialize the counters for all functions to 0. These counters are used to keep track of how many calculations were made inside each function. The counter variables are only increased if calculating a Factorial value greater than 1 since the base value is known. */ counterR = 0; /*used for RecursiveFac*/ counterL = 0; /*used for LoopFac*/ } public long RecursiveFac(long f) { /*Parameter long f : f is the Factorial number to calculate. It is assumed to be non-negative (no check made). It calculates the Factorial value by making a recursive calls to calculate RecursiveFac(n-1) and then multiplying by n. Return: the answer to Factorial of f. */ if(f<=1) /*base case*/ return f; else { counterR++; /*global var to determine # of calculations made*/ return RecursiveFac(f-1) * f; } }/*end RecursiveFac*/ public long LoopFac(long f) { /*Parameter long f : f is the Factorial number to calculate. Uses an iterative loop to calculate the Factorial values. Bottom up approach. Return: the answer to Factorial of f. */ long solution = 1; /*the most recent Fac value calculated.*/ if (f <= 1) return f; else { for(long i=2; i<=f; i++) { counterL = counterL + 1; solution = solution * i; }/*end for*/ } return solution; }/*end LoopFac*/ public static void main(String[] args) { Factorial f1 = new Factorial(); long solutionR = 0; /*solution to Recursive Fac*/ long solutionL = 0; /*solution to Loop Fac*/ solutionR = f1.RecursiveFac(10); solutionL = f1.LoopFac(10); if (solutionR == solutionL) /*functions worked*/ { System.out.print("The Solution to Fac(10) is: "); System.out.println(solutionR); System.out.println(); System.out.println("Here is the data for calculating factorial(10):"); System.out.println(); System.out.print("The Recursive Function made "); System.out.print(f1.counterR); System.out.println(" calculations."); System.out.print("The Iterative Function made "); System.out.print(f1.counterL); System.out.println(" calculations."); } else System.out.println("There is an error in the Fib functions."); Factorial f2 = new Factorial(); solutionR = f2.RecursiveFac(20); solutionL = f2.LoopFac(20); if (solutionR == solutionL) /*functions worked*/ { System.out.print("The Solution to Fac(20) is: "); System.out.println(solutionR); System.out.println(); System.out.println("Here is the data for calculating factorial(20):"); System.out.println(); System.out.print("The Recursive Function made "); System.out.print(f2.counterR); System.out.println(" calculations."); System.out.print("The Iterative Function made "); System.out.print(f2.counterL); System.out.println(" calculations."); } else System.out.println("There is an error in the Fac functions."); Factorial f3 = new Factorial(); solutionR = f3.RecursiveFac(30); solutionL = f3.LoopFac(30); if (solutionR == solutionL) /*functions worked*/ { System.out.print("The Solution to Fac(30) is: "); System.out.println(solutionR); System.out.println(); System.out.println("Here is the data for calculating Factorial(30):"); System.out.println(); System.out.print("The Recursive Function made "); System.out.print(f3.counterR); System.out.println(" calculations."); System.out.print("The Iterative Function made "); System.out.print(f3.counterL); System.out.println(" calculations."); } else System.out.println("There is an error in the Fib functions."); }/*end main*/ private long counterL; private long counterR; }/*end class Factorial*/