Fibonacci numbers

Fibonacci numbers are a famous sequence where each number is made by adding together the previous two, starting with 0 and 1.

What happens if we start with different input values?


In [ ]:
F0 = 0
F1 = 1
N = 20  # How many numbers to generate

In [ ]:
print(F0)
a, b = F0, F1
for _ in range(N):
    a, b = b, a + b
    print(a)

Dividing consecutive Fibonacci numbers gives numbers which get closer to the golden ratio.


In [ ]:
ratios = []
a, b = F0, F1
for _ in range(N):
    a, b = b, a + b
    ratios.append(b / a)

In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt

In [ ]:
from math import sqrt
plt.plot(ratios)
Φ = (1 + sqrt(5)) / 2   # The golden ratio
plt.hlines(Φ, xmin=0, xmax=N, linestyles='dotted')