The Fibonacci sequence

The Fibonacci sequence is the sequence of numbers generated by applying the rules: $$ a_1 = a_2 = 1, \quad a_i = a_{i-1} + a_{i-2}. $$ That is, the $i$th Fibonacci number is the sum of the previous two: $1, 1, 2, 3, 5, 8, 13, \cdots$.

Here are two ways of generating the Fibonacci sequence. Firstly, by appending to a list:

# Calculates and stores the first n Fibonacci numbers

n = 100
fib = [1, 1]
for i in range(2, n+1):
    fib.append(fib[i-1] + fib[i-2])
print(fib)

Alternatively, we can generate the sequence without storing more than two numbers at a time as follows:

# Calculates the first n Fibonacci numbers
n = 100
# Keep track of the two most recent Fibonacci numbers
a, b = 1, 1
print(a, b, end='')
for i in range(2, n+1):
    # The next number (b) is a+b, and a becomes the previous b 
    a, b = b, a+b
    print(' ', b, end='')