Programming Challenge: Fibonacci Sequence

đź“… August 5, 2015
fibthmbToday’s programming challenge involves writing the Fibonacci sequence in Python 3. Most computer programming books present this exercise in algorithms, and it is a good one.

There are a number of ways to achieve a Fibonacci sequence. Some involve recursive functions (functions that call themselves until a condition is met), but you are free to write the program in any way you like as long as the resulting code is readable, properly commented, and produces the correct sequence. You must also give a written explanation about how your code produces the sequence.

Download PDF: fibonacci_sequence

Try to solve this exercise for yourself before looking at the solution.


The resulting sequence should be:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

If starting from 0, then you should see:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

(The commas were added for clarity even though the code shown below does not print them.)

Other possible solutions exist, but here is a classic code solution written in a Python style. The goal shows the steps without being too terse. To keep the code listing short, I will explain the process separately.

#!/usr/bin/env python3
# Kickstart the sequence.
a, b = 1, 1
# Main loop
while a < 100:
   print(a, end = ' ')
   a, b = b, a + b
# Add a new line

How It Works

a, b = 1, 1

Kickstart the sequence. We must begin somewhere, so we specify two starting values because we want to add these two values together during each loop iteration. Variable a is the first number, and variable b is the consecutive number. We need two variables to keep track of the sequence. Think of the values as “external” and “internal.” a is the external value that we see when we print it, and b is the internal value that keeps track of the number we will print next.

while a < 100:
   print(a, end = ' ')
   a, b = b, a + b

This is the main loop that iterates until the value of a exceeds 100. We only print a, not b. b is used for internal calculations.

print(a, end = ' ')

The first thing to do is print a, whatever its value might be. Since we want to print the sequence on one line without carriage returns, we use end = ‘ ‘ to separate the numerals with one space. Feel free to use whatever symbol you like. You can even use a comma plus a space, but keep in mind that with this simple code, a comma will appear after the last numeral in the sequence.

a, b = b, a + b

This is the Python way of assigning two values to two separate variables at once in the same statement. a becomes b, and b becomes a + b, which is the next number in the sequence. For example, if we begin with a = 1 and b = 1, the sequence so far is,

1, 1

At this point you might be wondering, “Why is the program printing 1, 1 and not 1, 2 when we are only printing a?

Only a is printed, so 1 always appears first since that is its starting value. Then, b, which has a value of 1 in the beginning, is assigned to a, so a still has a value of 1 after the assignment. When a is printed on the second iteration, it prints 1. This is why we see 1, 1 in the beginning of the sequence.

b = a + b is how we obtain the next number in the sequence and assign it to b.


When the value of a is greater than 100, the loop exists and a newline is printed so the terminal prompt will appear on a new line.

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: