1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Mathematically, we write it as:
F(n) = F(n) + F(n-1)
This can easily be done in Python, using generators.
def Fibonacci(n):
a, b = 1, 1
while b < n:
yield b
a, b = b, a + b
To print all the Fibonacci numbers less than 4000000,a, b = 1, 1
while b < n:
yield b
a, b = b, a + b
for i in Fibonacci(4000000):
print i,
Output:print i,
>>> 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578