Complimenting Fibonacci number coding – iterative and recursive approach, we can improve performance by caching. If you run this

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class RecursiveFibonacci { public int fibonacci(int n) { if (n == 0 || n == 1) return n; System.out.println("evaluating fibonacci(" + n + ")"); return fibonacci(n - 2) + fibonacci(n - 1); } public static void main(String[] args) { int nThfibonacciNo = new RecursiveFibonacci().fibonacci(5); System.out.println(nThfibonacciNo); } } |

Output

| evaluating fibonacci(5) evaluating fibonacci(3) evaluating fibonacci(2) evaluating fibonacci(4) evaluating fibonacci(2) evaluating fibonacci(3) evaluating fibonacci(2) 5 |

and you can see “fibonacci(3)” is repeated 2 times, “fibonacci(2)” is repeated 3 times, and so on. If you pick a larger number like 21, there will be…...

## Members Only Content

This content is for the members with any one of the following paid subscriptions:

30-Day-Java-JEE-Career-Training, 90-Day-Java-JEE-Career-Training, 180-Day-Java-JEE-Career-Training, 365-Day-Java-JEE-Career-Training, 60-Day-Java-JEE-Career-Training, 2-Year-Java-JEE-Career-Training and 10-Day-Java-Interview-Refresher
Want to evaluate the quality of the contents to see if they will add value to you?

Click Here and check the contents with

^{Try}.

Log In |

Register