The Fibonacci Test: What AI Gets Right and What It Misses
November 23 is 11/23, which makes today Fibonacci Day: 1, 1, 2, 3.
Ask any AI coding assistant for a Fibonacci function and you will get a correct one in seconds. Correct is not the hard part. The same answer can be correct and still 196,000 times slower than it needs to be, or crash outright before n reaches 500. We measured four correct Python versions to show where the real engineering decision lives.
The short answer
Use a simple loop (iteration) for almost all Fibonacci code: it is fast, uses constant memory and never hits a recursion limit. Switch to fast doubling when n runs into the tens of thousands or more. Never ship naive recursion past roughly n = 30, and watch memoized recursion, which crashed at about n = 500 in our tests.
Four correct ways to compute Fibonacci in Python
Every version below returns the right number. They just send very different bills.
Naive recursion: O(φⁿ) time
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
It reads exactly like the math, which is why assistants and textbooks love it. It also recomputes the same values over and over, so the work grows exponentially. At n = 30 it makes over a million calls.
Memoized recursion with lru_cache: O(n) time, O(n) space
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
One decorator turns exponential into linear. This is the answer that looks smartest in a code review. It is also the one hiding a wall, which we’ll get to.
Iteration: O(n) time, O(1) space
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
Two variables, one loop, no stack. Boring in the best way.
Fast doubling: O(log n) arithmetic steps
def fib(n):
def pair(k):
if k == 0:
return (0, 1)
a, b = pair(k >> 1)
c = a * (2 * b - a)
d = a * a + b * b
return (d, c + d) if k & 1 else (c, d)
return pair(n)[0]
Fast doubling uses two identities, F(2k) = F(k)·(2F(k+1) − F(k)) and F(2k+1) = F(k)² + F(k+1)², to halve n at each step. It is the method most tutorials mention and skip. Its recursion depth is only about log₂ n, so even n = 1,000,000 needs around 20 levels.
We timed them: benchmark results
Method: Python timeit.repeat, best of 5 runs, time per call. Memoized timings build a fresh cache on every call, so they measure real work, not a lookup.
Machine: Apple M4 Pro, macOS 26.7, Python 3.9.6 (the Apple Command Line Tools build). Run 17 Nov 2026. Newer Python versions will be somewhat faster; the ratios are the point.
| Method | n | Time per call |
|---|---|---|
| Naive recursion | 20 | 0.90 ms |
| Naive recursion | 30 | 112 ms |
| Memoized (lru_cache) | 30 | 4.7 µs |
| Memoized (lru_cache) | 400 | 63 µs |
| Memoized (lru_cache) | 500 | RecursionError |
| Iteration | 30 | 0.57 µs |
| Iteration | 1,000 | 27 µs |
| Iteration | 10,000 | 1.07 ms |
| Iteration | 100,000 | 86 ms |
| Fast doubling | 30 | 0.79 µs |
| Fast doubling | 1,000 | 2.2 µs |
| Fast doubling | 10,000 | 22 µs |
| Fast doubling | 100,000 | 0.86 ms |
| Fast doubling | 1,000,000 | 36 ms |
Three things jump out:
- At n = 30, naive recursion is about 196,000 times slower than iteration (112 ms against 0.57 µs).
- At n = 100,000, fast doubling is about 100 times faster than iteration (0.86 ms against 86 ms). F(100,000) has 20,899 digits; F(1,000,000) has 208,988, and fast doubling still returns it in 36 ms.
- At small n, the plain loop wins. Fast doubling’s extra arithmetic costs more than it saves until n passes about 60.
All four methods returned identical values wherever they ran. Want to check our work? The full script is at the end of this post.
Why does lru_cache Fibonacci hit a recursion error?
Memoized Fibonacci still recurses all the way down before the cache can help, and lru_cache adds its own wrapper call to every level. Python’s default recursion limit is 1,000 frames, so on a cold cache our version failed at n = 500 on Python 3.9 when called directly (a little lower inside deeper call stacks; the exact point varies by Python version), about half the depth most people expect.
You can raise the ceiling with sys.setrecursionlimit, but that is a band-aid. Push it too far and you risk crashing the interpreter itself instead of getting a clean error. The real fix is a method that doesn’t need a deep stack: the loop, or fast doubling.
Which Fibonacci method should you use?
| Approach | Time | Space | Max safe n (default Python) | Use it for |
|---|---|---|---|---|
| Naive recursion | O(φⁿ) | O(n) stack | ~30 before it gets painfully slow | Explaining recursion, nothing else |
| Memoized (lru_cache) | O(n) | O(n) cache + stack | About 500 on a cold cache | Small n when you already need the cache |
| Iteration | O(n) | O(1) | No recursion limit | Default choice for almost all code |
| Fast doubling | O(log n) steps | O(log n) stack | No practical limit | Very large n, or many big lookups |
Fibonacci and AI coding assistants
What an assistant typically hands you
Ask a general-purpose assistant for “a Python Fibonacci function” and the first answer is often naive recursion or the lru_cache version, often with a tidy explanation of why it is elegant. It is not wrong. It answers the question exactly as asked.
The trouble is that nobody asked the real question: how big is n, how often does this run, and what happens when it fails? An assistant can’t see your production constraints unless you state them. That gap is where the engineer earns their keep.
Four questions to ask before you accept it
- How big will the input get? Fine at n = 25 can mean a frozen request at n = 40.
- What does it cost in memory? A cache that grows with n is a quiet leak in a long-running service.
- How deep does it recurse? In Python, depth is a hard ceiling, and ours arrived at about n = 500.
- What do big integers cost? Fibonacci numbers grow fast. Past a few thousand digits, the arithmetic itself, not the loop count, starts to dominate.
Ask those four and the assistant becomes very good at writing the version you actually need.
Why do interviewers still ask Fibonacci?
Because nobody needs the answer, everyone can discuss the tradeoffs. Fibonacci is small enough to write in a minute and rich enough to cover recursion, complexity, memory and edge cases. Interviewers are grading the conversation, not the function.
That is even more true as AI joins the room. More technical screens now let candidates use an AI assistant, which means the interview isn’t disappearing. It’s changing shape. In either format, the strong candidate is the one who questions the first answer, tests it, and explains which version they’d ship and why.
What this looks like on an enterprise team
On real projects, Fibonacci is a stand-in for every “it works on my machine” function: a report query that is fine on test data, a retry loop that is fine until the outage, a cache that is fine until month three. AI makes writing that code nearly free. Reviewing it is where the value moved.
At Smoothstack, that is the habit we hire and develop for: engineers who use AI assistants every day and still ask what the code costs at scale before it merges. It is the same judgment behind the skills every engineer will need, and it is what our clients count on when they build their teams with Smoothstack.
Run the benchmark yourself
import timeit
from functools import lru_cache
def naive(n): return n if n < 2 else naive(n-1) + naive(n-2)
def memo(n):
@lru_cache(None)
def f(k): return k if k < 2 else f(k-1) + f(k-2)
return f(n)
def it(n):
a, b = 0, 1
for _ in range(n): a, b = b, a + b
return a
def fd(n):
def g(n):
if n == 0: return (0, 1)
a, b = g(n >> 1); c = a*(2*b - a); d = a*a + b*b
return (d, c + d) if n & 1 else (c, d)
return g(n)[0]
def t(f, n, num):
return min(timeit.repeat(lambda: f(n), number=num, repeat=5)) / num
print(t(naive, 30, 3), t(it, 30, 200), t(fd, 100000, 200), t(it, 100000, 20))
FAQ
Is recursion or iteration faster for Fibonacci?
Iteration. In our Python 3.9.6 benchmark on an Apple M4 Pro, a simple loop computed F(30) in 0.57 microseconds, while naive recursion took 112 milliseconds, roughly 196,000 times slower. Memoized recursion closes most of the gap but still trails the loop and adds recursion-depth risk.
What is the fastest way to compute Fibonacci in Python?
For large n, fast doubling. It needs only O(log n) arithmetic steps, and in our test it computed F(100,000) in 0.86 milliseconds versus 86 milliseconds for a loop, about 100 times faster. For small n, under about 60, the plain loop is just as fast or faster.
Why does lru_cache Fibonacci hit a recursion error?
Memoized recursion still has to recurse to the bottom before the cache helps, and lru_cache adds a wrapper call at each level. With Python’s default limit of 1,000 frames, our version raised RecursionError at about n = 500 on a cold cache in Python 3.9. Use a loop or fast doubling instead of raising the limit.
What is the time complexity of recursive Fibonacci?
Naive recursive Fibonacci runs in exponential time, about O(φⁿ) where φ ≈ 1.618, because it recomputes the same subproblems repeatedly. Adding memoization drops it to O(n) time and O(n) space. Iteration is O(n) time with O(1) space, and fast doubling needs O(log n) arithmetic steps.
Why do interviewers ask Fibonacci?
It is short enough to code in a minute and deep enough to test recursion, complexity, memory, edge cases and communication. Interviewers rarely care about the number itself. They want to hear you compare approaches, spot the exponential blowup, and explain which version you would ship for a given input size.
Should I use an AI assistant in a coding interview?
Only if the interviewer allows it, and many AI-assisted rounds now do. If you use one, treat its first answer as a draft: ask about input size, memory and recursion depth, test edge cases, and explain your changes out loud. The judgment you show is what gets scored, not the code it typed.
Engineers who ask “what does this cost at scale?” do well here. See open roles at Smoothstack, or talk to us about how to build your team with engineers who think this way.
Let’s Build Your Team
Connect with the Smoothstack team to learn how to close your digital skills gap with a custom-trained team.