2 ms·
To be fair, here's plain English (from Wikipedia, re-entered by me): The sequence f(n) of Fibonacci numbers is defined by the recurrence relation: f(n) = f(n-
by analog31 9y ago
To be fair, here's plain English (from Wikipedia, re-entered by me):
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:
f = [0, 1]
for i in range(100):
f.append(f[-1] + f[-2])
- joshuamorton 9y agoheck, 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.