Answered step by step
Verified Expert Solution
Question
1 Approved Answer
java Recursion. public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n
java Recursion.
public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); }
I'm confused with the last line especially because if n = 5 for example, then fibonacci(4) + fibonacci(3) would be called and so on but I don't understand how this algorithm calculates the value at index 5(or index 6,7...) by this method. Please explain.
So if we substitute 5 in fibonacci method, fibocanni(5)
n = 5 =>>
=>> This is how i think it works : fibonacci(5-1) + fibonacci(5-2) = 7 but this calculation is wrong because it should be 8 instead of 7.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started