Categories: TECHNOLOGY

Using Python to Generate a Fibonacci Sequence

The first two numbers in a Fibonacci sequence are 0 and 1. Each subsequent Fibonacci number is the sum of its preceding two. For example, in the Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, and 13, the third number (1) is 0 plus 1, the fourth number (2) is 1 plus 1, the fifth number (3) is 1 plus 2, the sixth number (5) is 2 plus 3, the seventh number (8) is 3 plus 5, the eighth number (13) is 5 plus 8. You can continue this indefinitely.

You can use a simple Python program, Fibonacci.py, to generate and display a Fibonacci sequence, and display the ratio between each two sequential numbers. For all except the smallest Fibonacci numbers, this ratio approximates the Golden ratio.

Program Overview – As shown in the first image above, Fibonacci.py executes as follows:

1 – Fibonacci.py prompts you to enter an integer, n, and then initializes variables i, a, b, c, and d.

2 – While i is less than n, the ‘while’ loop displays the current iteration count, the current Fibonacci number and the ratio of the current Fibonacci number to the previous.

3 – The ‘while’ loop increments the counter, i.

4 – If i is greater than 1, the ‘while’ loop calculates the ratio of b to a because it is running its third (or greater) iteration. As shown in the second image above, there is no ratio for the first two iterations. As shown in the third image above, as the Fibonacci numbers grow larger, the ratio approaches the Golden ratio, 1.618034.

5 – The ‘while’ loop adds a and b to calculate c, which is the next Fibonacci number.

6 – The ‘while’ loop sets a equal to b to reassign the old second (current) Fibonacci number as the new first.

7 – The ‘while’ loop sets b equal to c to reassign the next Fibonacci number as the new second (current).

8 – The ‘while’ loop repeats steps 2 through 7 until i is equal to n, which terminates the loop and the program.

Creating your Fibonacci Generator – To create your Fibonacci.py Python program, do the following:

1 – Install both Python 3 and IDLE for Python 3.

2 – Start IDLE, click “File” and then click “New Window.”

3 – Click “File,” click “Save As,” type “Fibonacci” into the File-name field, select a convenient location (such as Desktop) and then click “Save.”

4 – Do either of the following:

* – Copy FibonacciGen and paste it into your Fibonacci.py window. For more information, see Running Python Examples.

* – Click on the first image above to expand it, copy it to a word-processor page, print that page and then type the code into your Fibonacci.py window.

5 – Run your Fibonacci.py program according to the section below.

Running your Fibonacci Generator – You can run Fibonacci.py to generate a Fibonacci sequence according your specified iterations. To run your Fibonacci.py Python program, do the following:

1 – Create Fibonacci.py according to the section above.

2 – Start IDLE, click “File,” click “Open,” navigate to and click “Fibonacci.py.”

3 – Click “Run,” click “Run Module” and then enter an integer to display, in the Python Shell, all Fibonacci numbers within your specified number of iterations. For example, as shown in the third image above, if you specify 30 iterations, Fibonacci.py generates a Fibonacci sequence with 30 numbers.

Sources:

* – Dan Reich, “Fibonacci Sequence, Spirals and the Golden Mean”, Department of Mathematics, Temple University

* – Mark Freitag, “Phi: That Golden Number”, University of Georgia

Karla News

Recent Posts

Inside “Pedagogy of the Oppressed”

The Brazilian author Paulo Freire in his writings of "Pedagogy of the Oppressed," first published…

53 seconds ago

Exploring the Medical Administrative Assistant Career Path

The income prospects for medical administrative assistants will also be good now, and in the…

6 mins ago

The Versatile Yuca Root

Yuca (pronounced you-kah), also known as cassava, is a root vegetable that is grown in…

12 mins ago

Best Romantic Restaurants in Aruba

Aruba can be a wonderfully romantic getaway for couples. There are lots of things to…

18 mins ago

25 Random Acts of Kindness Ideas

I'm no saint. In fact, I'm not even sure that I'm a good person. However,…

23 mins ago

Top 5 Free Online Calorie Counters

If you are watching your calories, you can keep track of your daily calorie intake…

28 mins ago

This website uses cookies.