Fibonacci: Naive vs. Memoized

Watch a program waste an absurd amount of work, then fix it with one line.

Everyone knows Fibonacci as nature's number. Seashells, sunflowers, daisies, it's the pretty pattern hidden in everything. If that's all you know about it, you should sue your math teacher. Because Fibonacci isn't just beautiful. It's one of the fastest-growing sequences we have. And that fact changes everything about how you should think about computing it.

Here's why growth matters. Imagine three people, each starting with the number three and a piece of paper. Person A will add one billion every time we say "go." Person B will multiply by ten. Person C will follow the Fibonacci sequence. After a few rounds, watch what happens: Person A creeps ahead, then falls behind. Person B catches up, then also falls behind. Person C? The Fibonacci sequence explodes. It doesn't add or multiply, it compounds. Each new number is the sum of the two before it, which means it grows faster than anything you can do by just adding or multiplying. Mathematicians call this exponential growth. What matters is that it compounds.

Now apply that to a real problem: compute the 1000th Fibonacci number. There's a dumb way to do it and a smart way. The dumb way is to recalculate the entire sequence from scratch every time, the same work, done over and over, thousands of times. The smart way is to remember what you've already calculated and build on it. One stores everything. The other throws it away and starts again.

Remember: each Fibonacci number is the sum of the two before it. To calculate fib(1000), you need fib(999) + fib(998). To get those, you need fib(998) + fib(997), and fib(997) + fib(996). And so on.
Compute fib( 8 )
Speed 5
Naive calls
0
Memoized calls
0
Naive Recursion
Red nodes are recalculated, the same value computed again and again. Watch how fib(3) gets calculated dozens of times.
Memoized
Cyan nodes are retrieved from cache, computed once, then looked up instantly. The tree stays flat.
Current call
First time computing this value
Already computed (wasted work)
Cache hit (retrieved, not recomputed)

It's tempting to think about this as just a coding trick: memoization, caching, storing values to avoid recomputation. But the real lesson is older and bigger than that. Storing information, whether in libraries, data centers, or the minds of people who've worked a problem for twenty years, is how you avoid paying the price of recalculation.

When a company in 2024 lays off a team that's been there for decades to cut costs, what they're actually doing is deleting their cached knowledge. They're forcing the next team to start over: relearn risks already navigated, patterns already discovered and the thousands of small decisions that never made it into any document because the person who made them was still in the room. A person who's worked a role for that long hasn't just learned things. They've developed the capacity to recognize patterns, to extrapolate from one problem to a thousand variations of it that haven't arrived yet.

Losing that isn't just losing a database. It's losing the training of a pattern-recognition engine that was built over years. The cost of rebuilding it from scratch is exactly the cost of computing Fibonacci the dumb way: you're redoing work that was already done, and you'll keep redoing it until you remember to store what matters.

See the actual code running on this page

  
matthewkmeyer.com / projects