I have the recursion method of Fibonacci. I am trying to get the number of calls for the method. When the index are 3, 4, 5, 6 and so on, the count output should be 3, 5, 9, 15 and so on. My code is giving me wrong outputs. Maybe my for-loop is affecting it? Please help!
import java.util.*;
public class RecursionCallsFib{
private static int count;
public static int rabbit(int n) { //Fibinocci method
count++;
if (n <= 2) {
return 1;
}
else {// n > 2, so n-1 > 0 and n-2 > 0
return rabbit(n-1) + rabbit(n-2);
}
}
public static void main(String [] args){
System.out.println("Index" + "\t" + "Value" + "\t" + "Count");
for(int p=1;p<=15;p++){
System.out.println(p + "\t" + rabbit(p) + "\t" + count);
}
}
}