In [1]:
def fibonacci(n):
a, b = 0, 1
while n:
a, b = b, a + b
n -= 1
return a
In [2]:
for n in range(10):
print(fibonacci(n))
In [3]:
[fibonacci(n) for n in range(10)]
Out[3]:
In [4]:
def gen_fibo(n):
a, b = 0, 1
while n:
yield a
a, b = b, a + b
n -= 1
In [13]:
g10 = gen_fibo(10)
g10
Out[13]:
In [14]:
f10 = list(g10)
f10
Out[14]:
In [15]:
for n in gen_fibo(n):
print(n)
In [7]:
f10 = list(gen_fibo(10))
ratios = [a/b for a, b in zip(f10[2:], f10[1:-1])]
ratios
Out[7]:
In [8]:
φ = (1 + 5**0.5) / 2
φ
Out[8]:
In [10]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure()
plt.plot(ratios)
plt.axhline(φ, color='r')
Out[10]: