Last active 1727095887

Ever ask the same question over and over? Annoying, right? That’s what your computer thinks too.

Revision f94338439caa08d8faa2e0176497f34662c547aa

memoization.py Raw
1# example of memoization in python for fibonacci
2
3# Memoization: Let’s Be Efficient, Shall We?
4# Ever worry about recursion and the stack and so on...
5# Ever ask the same question over and over?
6# Annoying, right? That’s what your computer thinks too.
7# Memoization fixes this by storing results so you don’t have to repeat expensive calculations.
8def fib(n, memo={}):
9 if n in memo:
10 return memo[n]
11 if n < 2:
12 return n
13 memo[n] = fib(n - 1, memo) + fib(n - 2, memo)
14 return memo[n]
15
16fib(100)
17
18# 354224848179261915075
19