Results 1 to 6 of 6
  1. #1
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default Fibonacci sequence using iterative and recursive method

    I am to produce the fibonacci sequence using one parameter, then two parameters. I am also supposed to do it with an iterative and recursive method, I know im close by using the general formula for fibonacci: f(x)=f(x-1)+f(x-2)
    But im having trouble with the base case:

    Java Code:
    public class Lab21_7 {
    
        public static void init(long[] arr) {
            // Initialize all the entries of the array to 0
            for (int i = 0; i < arr.length; i++) {
                arr[i] = 0;
            }
        }
    public static long fib(int max) {
            // sumAll basic version
            System.out.println("Called Fibonacci(" + max + ")");
    
            long result;
            
                if (max <= 1)       //<----this part messes me up
                result = 1;
                    
            else
                result = fib(max-2)+fib(max-1);
            System.out.println("Return Fibonacci(" + max + ") = " + result);
            
            return result;
      public static long fib(long[] arr, int max) {
            // Simple Recursion with array
            System.out.println("Called Fibonacci(" + max + ")");
            
            if (max <= 1) 
                arr[max] = 1;
            else if (arr[max] == 0)
                arr[max] = fib(max-2) + fib( arr,max - 1);
            
            System.out.println("Return Fibonacci(" + max + ") = " + arr[max]);
            
            return arr[max];
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,875
    Rep Power
    8

    Default

    What does your code do instead? You might consider posting an SSCCE that we can run.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Take a look here for help with fibonacci numbers.

  4. #4
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    My current code prints out the incorrect fibonacci sequence, I implemented the fibonacci code from the wikipedia website and it returned the correct sequence, except there were gaps in the sequence. i.e. 1,1,2,3,5,0,0,8.....

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,875
    Rep Power
    8

    Default

    Like I said, you might want to post an SSCCE that shows us what you're actually doing. We can't see how you're calling these methods to produce that output, so we can't really guess at what your problem is.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2011, 03:16 AM
  2. Help needed to turn a recursive method into iterative
    By mercyfulfate in forum New To Java
    Replies: 5
    Last Post: 07-16-2010, 06:13 PM
  3. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 06:46 AM
  4. Fibonacci sequence
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 06:59 AM
  5. Replies: 3
    Last Post: 02-09-2010, 05:22 AM

Posting Permissions

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