Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Fibonacci using recursion

    I posted this in another thread because the thread starter person has the exact assignment as I do apparently, but I can't hijack his thread. Sorry I'm new to this site. But anyway, here is what I got:

    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) {
    // fibonacci basic version
    System.out.println("Called fibonacci(" + max + ")");

    long result;
    if (max <= 1)
    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(arr, max-2)+ fib(arr, max - 1);

    System.out.println("Return fibonacci(" + max + ") = " + arr[max]);

    return arr[max];
    }

    public static void main(String[] args) {

    final int MAXSIZE = 100;
    long[] arr = new long[MAXSIZE];

    System.out.println("Fibonacci Number 5 is " + fib(5));
    System.out.println("Fibonacci Number 10 is " + fib(10));
    System.out.println("Fibonacci Number 20 is " + fib(20));
    System.out.println("Fibonacci Number 15 is " + fib(15));
    System.out.println();

    init(arr);
    System.out.println("Fibonacci Number 5 is " + fib(arr, 5));
    System.out.println("Fibonacci Number 10 is " + fib(arr, 10));
    System.out.println("Fibonacci Number 20 is " + fib(arr, 20));
    System.out.println("Fibonacci Number 15 is " + fib(arr, 15));
    System.out.println();

    I'm close I know it. The only hint I've got is that I need to 'return values'.

  2. #2
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Oh that's right. Its supposed to output something similar to this, but there should be called and returned statements, as shown in the code I put up. The values should be the same though.

    Fibonacci 2 value 1
    Fibonacci 3 value 2
    Fibonacci 4 value 3
    Fibonacci 5 value 5
    Fibonacci Number 5 is 5
    Fibonacci 2 value 1
    Fibonacci 3 value 2
    Fibonacci 4 value 3
    Fibonacci 5 value 5
    Fibonacci 6 value 8
    Fibonacci 7 value 13
    Fibonacci 8 value 21
    Fibonacci 9 value 34
    Fibonacci 10 value 55
    Fibonacci Number 10 is 55
    Fibonacci 2 value 1
    Fibonacci 3 value 2
    Fibonacci 4 value 3
    Fibonacci 5 value 5
    Fibonacci 6 value 8
    Fibonacci 7 value 13
    Fibonacci 8 value 21
    Fibonacci 9 value 34
    Fibonacci 10 value 55
    Fibonacci 11 value 89
    Fibonacci 12 value 144
    Fibonacci 13 value 233
    Fibonacci 14 value 377
    Fibonacci 15 value 610
    Fibonacci 16 value 987
    Fibonacci 17 value 1597
    Fibonacci 18 value 2584
    Fibonacci 19 value 4181
    Fibonacci 20 value 6765
    Fibonacci Number 20 is 6765
    Fibonacci 2 value 1
    Fibonacci 3 value 2
    Fibonacci 4 value 3
    Fibonacci 5 value 5
    Fibonacci 6 value 8
    Fibonacci 7 value 13
    Fibonacci 8 value 21
    Fibonacci 9 value 34
    Fibonacci 10 value 55
    Fibonacci 11 value 89
    Fibonacci 12 value 144
    Fibonacci 13 value 233
    Fibonacci 14 value 377
    Fibonacci 15 value 610
    Fibonacci Number 15 is 610
    Fibonacci[2] value 1
    Fibonacci[3] value 2
    Fibonacci[4] value 3
    Fibonacci[5] value 5
    Fibonacci Number 5 is 5
    Fibonacci[6] value 8
    Fibonacci[7] value 13
    Fibonacci[8] value 21
    Fibonacci[9] value 34
    Fibonacci[10] value 55
    Fibonacci Number 10 is 55
    Fibonacci[11] value 89
    Fibonacci[12] value 144
    Fibonacci[13] value 233
    Fibonacci[14] value 377
    Fibonacci[15] value 610
    Fibonacci[16] value 987
    Fibonacci[17] value 1597
    Fibonacci[18] value 2584
    Fibonacci[19] value 4181
    Fibonacci[20] value 6765
    Fibonacci Number 20 is 6765
    Fibonacci Number 15 is 610

Similar Threads

  1. Fibonacci Help
    By zit1343 in forum New To Java
    Replies: 16
    Last Post: 01-24-2011, 01:09 AM
  2. Fibonacci sequece
    By Bgreen7887 in forum New To Java
    Replies: 10
    Last Post: 12-03-2010, 07:27 PM
  3. help with fibonacci
    By likemine in forum New To Java
    Replies: 8
    Last Post: 01-07-2010, 02:32 AM
  4. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  5. help with fibonacci problem
    By thekrazykid in forum New To Java
    Replies: 4
    Last Post: 12-12-2008, 10:41 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
  •