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:
30DayJavaJEECareerCompanion, 90DayJavaJEECareerCompanion, 180DayJavaJEECareerCompanion, 365DayJavaJEECareerCompanion and 2YearJavaJEECareerCompanion
Feel free to evaluate the quality of the contents
Free FAQs 
Home^{Try}
Log In 
Register