Remember how naive Fibonacci recomputes values?
You can fix that by caching results: python def fib_memo(n, cache={}): if n in cache: return cache[n] if n <= 1: return n cache[n] = fib_memo(n - 1, cache) + fib_memo(n - 2, cache) return cache[n] print(fib_memo(50)) # Fast! 12586269025 This technique, called memoization, stores computed results to avoid redundant work.
Python has a built-in decorator for this: python from functools import lru_cache @lru_cache(maxsize=None) def fib(n): if n <= 1: return n return fib(n - 1) + fib(n - 2) print(fib(100)) # Instant! You'll learn more about this in dynamic programming.