1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/usr/bin/env python
# Recursive function.
# This is considered to be the most elegant way!
def fib ( n ):
if n < 2:
return 1
else:
return fib(n-1) + fib(n-2)
for x in range(20):
print fib(x)
# Looping over a list.
# If one is not interested in the n. Fibonacci number, but in the
# sequence up to the n. number, this might be faster.
L = 20
x = range(0, L)
for i in range(2, L):
x[i] = x[i-2] + x[i-1]
print x[1:]
|