Number of calls for nth Fibonacci number
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The naive recursive Fibonacci function is a classic example of how elegant code can hide expensive behavior. Counting the number of function calls is a good way to see why the simple recursive solution becomes impractical so quickly, even for moderate values of n.
Define the Call-Count Recurrence
Start with the usual recursive Fibonacci definition:
If C(n) is the total number of times fib is called while computing fib(n), then:
- '
C(0) = 1' - '
C(1) = 1' - '
C(n) = C(n - 1) + C(n - 2) + 1'
The extra + 1 counts the current call itself. This recurrence looks almost like Fibonacci because the recursion tree repeats the same shape again and again.
Measure Calls Directly
A concrete counter makes the growth easier to understand:
The first few call counts are:
- '
C(0) = 1' - '
C(1) = 1' - '
C(2) = 3' - '
C(3) = 5' - '
C(4) = 9' - '
C(5) = 15'
The growth is already steep. By the time you reach larger inputs, most of the work is repeated computation of the same smaller Fibonacci values.
Closed Form Relationship
There is a neat identity:
- '
C(n) = 2 * F(n + 1) - 1'
Here F(n) is the Fibonacci sequence itself with F(0) = 0 and F(1) = 1. You can verify the pattern by comparing small values. For example, when n = 5, F(6) = 8, so 2 * 8 - 1 = 15, which matches the measured call count.
That identity shows why the call count grows exponentially. Fibonacci numbers grow exponentially, so the number of recursive calls does too.
Why So Many Calls Happen
The inefficiency comes from overlapping subproblems. Computing fib(5) expands into fib(4) and fib(3), but fib(4) itself also calls fib(3). The same subtree is recomputed many times.
You can visualize it by printing the recursive expansion:
Even for 4, the tree clearly repeats calls such as fib(2) and fib(1). The cost is not hidden. It is duplicated everywhere in the tree.
Memoization Changes the Count Completely
Once you cache previous results, each Fibonacci index is computed once and reused:
The recursive style stays intact, but the call count drops from exponential growth to roughly linear growth. That is the key lesson behind dynamic programming.
Iterative Fibonacci Avoids the Issue Entirely
If the goal is simply to compute Fibonacci numbers efficiently, iteration is better:
This version uses constant memory, avoids recursion depth issues, and does not waste time expanding a call tree at all.
Common Pitfalls
- Counting only the non-base recursive branches and forgetting to include the current call itself.
- Assuming the naive recursive version is merely “a little slower” instead of exponentially more expensive.
- Using Fibonacci recursion as a benchmark without realizing interpreter overhead can blur the underlying algorithmic lesson.
- Implementing memoization but not actually reusing cached values correctly.
- Keeping the naive recursive solution in production code when the task only requires efficient numeric computation.
Summary
- The naive recursive Fibonacci call count follows
C(n) = C(n - 1) + C(n - 2) + 1. - A useful identity is
C(n) = 2 * F(n + 1) - 1. - The exponential growth comes from repeated evaluation of the same subproblems.
- Memoization reduces the effective work to linear growth in
n. - Iteration is usually the best practical solution when performance matters.
Related reading
- Number of Comparisons finding the median of 7 numbers
- Number of comparisons made in median of 3 function?
- Number of Increasing Subsequences of length k
- Number of largest element exchanges for quicksort
- Number of Partitions vs Producer Throughput in Apache Kafka
- NumPy grouping using itertools.groupby performance
- Number of sub-sequences in a given sequence
- Number of subarrays divisible by k

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.