3 ms·
heck, if you really want that: @functools.lru_cache def fibs(n): if n == 0: return 0 elif n == 1: return 1
by joshuamorton 9y ago
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.