# Fibonacci using recursion

• 03-02-2011, 08:07 PM
rootsoftheking
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'.
• 03-02-2011, 08:08 PM
rootsoftheking
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