Wrapped In Python – Edition 6 – Fibonacci Sequence

Generating the Fibonacci Sequence with Python

I have been insanely busy over the past month so I haven’t been spending as much time practicing python as I would have liked.  About a month ago, in my continued obsession with the beauty of mathematics, I gave myself the task of figuring out how to generate the Fibonacci Sequence using python.  I knew, python being what it is, that it would be a small amount of code to get it done.  Doing it on paper was easy; doing it with python didn’t immediately come to me.  I didn’t want to just Google the answer.  I’ll never learn to solve ‘real’ problems with python if I don’t force myself to think about how to do these things.  Lying in bed this evening, unable to sleep, I found myself thinking about it again and, as is so often the case, I happened a simple solution.  I’m sure there are many ways to generate the sequence in python; this is the one I figured out.

#!/usr/bin/env python

fibonacci = [0,1]

end = 0

while end < 98:
	fibonacci.append(fibonacci[(((len(fibonacci)-2)))]+fibonacci[(((len(fibonacci)-1)))])
	end += 1

for each in fibonacci:
	print each

I’m impressed that python allows it to be accomplished with such a short amount of code.  I was over-thinking it for a while.

Note: The formatting of the code showing above doesn’t look like the ‘fibonacci.append’ line is indented under the while statement. It is, though. If you copy and paste the code into your editor (I use Sublime Text 2) you will see the correct formatting.

The Fibonacci Sequence, if you didn’t already know is an ever-growing list of numbers generated by adding the two previous numbers on the list to one another (in order to get the next number on the list).  Modern mathmaticians tend to start with 0, 1 (Fibonacci, I have read, started with 1, 1).  Using the modern approach 0 +1 = 1.  This makes the list 0, 1, 1.  Now add 1 + 1 to get 2.  The list becomes 0, 1, 1, 2.  Now add 1 + 2 to get 3.  The list becomes 0, 1, 1, 2, 3.  Adding 2 +3 makes the list 0, 1, 1, 2, 3, 5.  Next you add 3 + 5 to get a list of 0, 1, 1, 2, 3, 5, 8.  Keep going forever.  Or until you get bored.  The numbers get big pretty quickly.  The 100th number in the sequence, for example, is 218,922,995,834,555,169,026.

Fibonacci numbers are hugely abundant in nature, especially in plants.  A supremely awesome discussion on this topic can be seen on YouTube.  It’s a 3-video series totaling about 15 minutes that everybody should watch.  Here:

Cheers,

Colin Weaver

If you liked this post, please consider sharing it.  Thanks!

About the Author

Colin Weaver

Colin Weaver is co-owner and lead instructor at ITdojo, Inc., a network security and information assurance training center and consulting firm located in Virginia Beach, VA. His passion for technology, networks, and security has led him to become enthralled with the idea of IPv6 and its implementation. In this blog he will share with you glimpses of what he has learned and a hint at what you’ll learn in his classes.