The Fibonacci sequence: each number is the sum of the two before it.
The naive recursive solution is elegant but extremely slow:
The fix: Memoization — cache results of previous calls:
This is your first taste of Dynamic Programming. Memoization = recursion + caching.
fibonacci(0)
0
fibonacci(5)
5
fibonacci(10)
55
fibonacci(20)
6765