Fibonacci series in Python

chris (2010-08-08 02:34:15)
1508 views
0 replies

This a quick and easy implementation of the Fibonacci series in Python. As demonstrated in the PHP version (where this python implementation was lifted from), you simply have to declare two inital variables instead of just one so that you start of with a pair of values to add together.

first = 0
second = 1
n = 27
# to be correct we will print the entire series which starts off 1, 1, 2 ...
print second
for i in range(n):
    next = first + second
    print next
    first = second
    second = next

Much cleaner than the php version, I think..


Digg it! Submit to Slashdot Add to Blinklist Del.icio.us Add to Newsvine Add to Technorati Add it to Google Bookmarks
comment