AlgoFrames

Calls in fib(5)

Warm-up · Trace the algorithm · about 4 min

Naive fib(5) calls itself again and again. Before the tree grows, predict how many calls it makes in total, the first call included.

Example 1

Input:

{"n":5}

Output:

5

fib(5) returns 5; the question is how many calls it takes.

Constraints

  • The trace uses n = 5.

Learn the idea first: Recursion trees: Fibonacci and repeated work, a visual lesson.

More Recursion trees: Fibonacci and repeated work problems