Consider the following recursive definition of :
fib(n) := if n = 0 then 1
else if n = 1 then 1
else fib(n-1) + fib(n-2)
The number of times is called (including the first call) for evaluation of is___________.
View worked solution
Answer: 41
Let denote the total number of times the function fib is called to evaluate .
Based on the recursive definition:
- For the base cases and , the function returns immediately without any further recursive calls:
- For , evaluating requires initial call plus the calls made during the evaluation of and :
We can compute the values of step-by-step:
Thus, the total number of times fib is called to evaluate is 41.
Free practice · Your answers on this page are not saved.
