The Fibonacci sequence starts with 0 and 1. After that, the th fibonacci number, denoted , is the sum of the prior two (). Here’s a table with the first few values in the sequence:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
---|---|---|---|---|---|---|---|---|---|
f() | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 |
Here’s some pseudocode for a method that calculates the th fibonacci number:
fib(3)
, how many times is fib
called in total (including the call to fib(3)
)?fib(4)
, how many times is fib
called in total?fib
?Find the two bugs in the following pseudocode. Hint: refer back to the four-step procedure for understanding recursive methods.
You asked to write a recursive method with the following specification:
Devise a recursive definition of the solution to this problem for all . Hint: consider using mod and integer division.
Write a line of code to replace the TODO comment in the method below to implement your recursive definition and correctly complete the method.