How do I use rFibNum and start the loop of trials with n= 40 and determine what value of n the program fails to complete. And how would I do this with memoizedFibonocc?
Thanks for the help
//Recursion: Fibonacci Number
import java.io.*; public class FibonacciNumberTest3 { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); static int callCountint = 0; static long callCountlong = 0; public static void main(String[] args) throws IOException { int n; long t; long nthFib; String callCountStr ; for(n = 2; n < 50; n++ ){ //stop if the size of the fib number exceeds size of int // this point was determined by trial and error if(n>44){ System.out.print("type enter to continue."); keyboard.readLine(); } String nStr = String.format("%1$3s", String.valueOf(n)); //initialize the array of memoized results for(int i = 0; i< 100; i++) solvedFibs[i] = -1; solvedFibs[0] = 0; solvedFibs[1] = 1; //remember the start time and calculate the term t = System.currentTimeMillis(); // pick one of the following recursion method calls nthFib =rFibNum( n); //nthFib =memoizedRecursion( n); //how long did it take t = System.currentTimeMillis() - t; //output the results System.out.print("The Fibonacci number " + nStr + " is: " + String.format("%1$12s", nthFib) ); if(callCountlong > 1000000000){ long oneBillion = 1000000000; callCountlong = callCountlong / oneBillion; callCountStr = Long.toString(callCountlong)+" billion "; }else{ if(callCountlong >= 1000000){ callCountlong = callCountlong / 1000000; callCountStr = Long.toString(callCountlong)+" million "; }else callCountStr = Long.toString(callCountlong); } //format the data into nice even columns //expand the strings by adding spaces to fill the column width // %1$24s will add spaces on the left making a 24 character column. String fibCalls = String.format("%1$12s",callCountStr); String callCountintStr = String.format("%1$12s",callCountint); System.out.println(" making ("+callCountintStr + ") "+fibCalls +" function calls taking "+t+" ms."); callCountint = 0; callCountlong = 0; }// end for loop }// end main public static long rFibNum(int n) { FibonacciNumberTest3.callCountint++; FibonacciNumberTest3.callCountlong++; if(n < 1) return 0; else if(n == 1) return 1; else return rFibNum( n - 1) + rFibNum( n - 2); }// end rFibNum /** * An array which stores calculated fibonacci numbers. */ static long[] solvedFibs = new long[100]; /** * Computes the nth fibonacci number using memoization */ static long memoizedRecursion(int n) { //some counters for the printout of results FibonacciNumberTest3.callCountint++; FibonacciNumberTest3.callCountlong++; if (n < 1) return solvedFibs[1]; // f(0) = 0 if (n == 1) return solvedFibs[2]; // f(1) = 1 // If the nth fibonacci number has not been calculated we calculate it. if (solvedFibs[n-2] < 0) solvedFibs[n-2] = memoizedRecursion(n-2); if (solvedFibs[n-1] < 0) solvedFibs[n-1] = memoizedRecursion(n-1); solvedFibs[n] = solvedFibs[n-1] + solvedFibs[n-2]; return solvedFibs[n]; }//end memoizedRecursion }