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:
30DayJavaJEECareerTraining, 90DayJavaJEECareerTraining, 180DayJavaJEECareerTraining, 365DayJavaJEECareerTraining, 60DayJavaJEECareerTraining and 2YearJavaJEECareerTraining
Evaluate the quality of the contents to see if they will add value to you?
Click Here and check the contents with
^{Try} OR look for post titles with ♥
Log In 
Register