Results 1 to 11 of 11
Like Tree2Likes
  • 2 Post By jim829

Thread: Recursion: Fibonacci Number Question

  1. #1
    judemartin99 is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    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?


    //Recursion: Fibonacci Number

    Java Code:
    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
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Recursion: Fibonacci Number Question

    Cross posted
    Recursion: Fibonacci Number Question (Java in General forum at JavaRanch)
    Recursion: Fibonacci Number Question

    db

    edit Habitual cross poster, doesn't return to threads started.
    Last edited by DarrylBurke; 10-05-2013 at 08:20 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    judemartin99 is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: Recursion: Fibonacci Number Question

    I did in fact cross post, because in order to show other members forums my question so they resolve my issue I am having trouble with. Instead of being
    the Forum Police or Watcher", you can offer some advice instead wasting your time seeing if members are cross posting.

    Thanks

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Recursion: Fibonacci Number Question

    Actually it is you who are wasting our time. By crossing posting you are soliciting information from multiple sources. This results in folks repeating information resulting in more duplicate and unnecessary posts.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    judemartin99 is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: Recursion: Fibonacci Number Question

    "who are wasting our time". Learn some grammar pal.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Recursion: Fibonacci Number Question

    Do you really think that bitchin about forum regulars is going to earn you brownie points and get any help?

    ignore++;

  7. #7
    judemartin99 is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: Recursion: Fibonacci Number Question

    @ Junky

    And you think you are going to enter brownie points for using bad words like "bitchin". c'mon man

    "NO BAD LANGUAGE" Forum Rules

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Recursion: Fibonacci Number Question

    You really are clueless!

  9. #9
    judemartin99 is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Default Re: Recursion: Fibonacci Number Question

    Please explain to me how I am clueless?

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Recursion: Fibonacci Number Question

    In my first post I advised you that whining about others and how they treat you is not productive and decreases your chances of getting help. Instead of taking that advice you decided to zone in on the "rude" word that I posted and continued your whining. Resulting in wasted time, no answer to your problem and even less chance of getting help. Please feel free to reply with some oh so biting comeback. It will be highly amusing!

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Recursion: Fibonacci Number Question

    Yep! Bad grammar on my part. Poor manners on your part. I'll settle for bad grammar any day.

    Regards,
    Jim
    DarrylBurke and doWhile like this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 11
    Last Post: 06-23-2013, 05:43 PM
  2. Fibonacci Number Calculator
    By Dr_DerpyHooves in forum New To Java
    Replies: 2
    Last Post: 11-28-2012, 03:56 AM
  3. Replies: 8
    Last Post: 01-21-2012, 02:14 AM
  4. Fibonacci using recursion
    By rootsoftheking in forum New To Java
    Replies: 1
    Last Post: 03-02-2011, 08:08 PM
  5. My first recursion method: the number e
    By Lil_Aziz1 in forum Reviews / Advertising
    Replies: 1
    Last Post: 01-23-2010, 11:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •