Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Recursion: Fibonacci Number Question

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursion: Fibonacci Number Question

    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
     
     
    }


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

  3. The Following 2 Users Say Thank You to Darryl.Burke For This Useful Post:

    copeg (October 5th, 2013), newbie (October 5th, 2013)

  4. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Recursion: Fibonacci Number Question

    F(n) = F(n-1) + F(n-2);

    F(0) = 0;
    F(1) = 1;


    At which value of n?

    add a throws clause (throws InputMismatchException) to your two methods (rfib and memorized).

    Then, when you call them in your for loop, put a try/catch block around each iteration like this


    try
    {


    }

    catch(InputMismatchException ime)
    {
    System.out.println(n);
    break;
    }


    It will stop your for loop though when it throws the exception. If you don't want it to stop when you reach that point and instead just tell them to hit enter, remove the "break;"
    Then it will just print out the n value where it stopped working.

  5. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion: Fibonacci Number Question

    not what I'm asking for exactly. How do I use rFibNum and start the loop of trials with n= 40

  6. #5
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Recursion: Fibonacci Number Question

    for(n = 40; n < 50; n++ )
    {
    ...........

    }

  7. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion: Fibonacci Number Question

    public class F {


    int fibo (int f) { if (f=0)return 0;
    else if (f=1) return 1;
    else return fibo(f-1)+fibo(f-2);
    }
    System.out.print(fibo(3));

    }

    What is incorrect in this code as the compiler show 5 errors in System.out.print..-some identifiers expected, illegal start of type

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. programming the Fibonacci number
    By johnmerlino in forum Algorithms & Recursion
    Replies: 3
    Last Post: July 25th, 2012, 11:22 PM
  3. Need help with a part in a recursion LinkedList question
    By ZenithX in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 2nd, 2011, 05:17 AM
  4. question about recursion..
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 16th, 2011, 09:51 AM
  5. Fibonacci generator and gives the number back as output
    By aurora in forum Algorithms & Recursion
    Replies: 1
    Last Post: June 20th, 2009, 12:58 AM