by andrewprock · 7y ago · 0 comments
Even the recursive python version with memoization is a bit difficult to read, but least it has the recurrence relation in it.
vals = {}
def fib(n):
if n < 2:
return n
if n in vals.keys():
return vals[n]
val = fib(n-1) + fib(n-2)
vals[n] = val
return valAdmittedly Python's facilities for doing that with some mutable state are pretty nice:
from itertools import islice
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
list(islice(fib(), 10)) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]I was speaking to readability.