Re:Computer History Museum 2018 Fellow: Guido van Rossum
by joshuamorton on Thursday February 22, 2018 @12:15PM
(#16439193)
heck, if you really want that:
@functools.lru_cache
def fibs(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibs(n-1) + fibs(n-2)
Technically you could also write it as def f(n):
if n in {0, 1}:
return n
return f(n-1) + f(n-2)
which is a bit trickier, but arguably better matches the way the seed values are described in english.
The sequence f(n) of Fibonacci numbers is defined by the recurrence relation:
f(n) = f(n-1) + f(n-2),
with seed values
f(0) = 0 and f(1) = 1
And a possible variant in Python: