Lompat ke konten Lompat ke sidebar Lompat ke footer

Creating Fibonacci Series on Python

Contents
1. What is Fibonacci Series?
2. Fibonacci Series Algorithm
3. Script Python for Generating Fibonacci Series
4. Conclusion

Fibonacci is a mathematician who was known as the most talented western mathematician in middle age. One of his excellent work is Fibonacci series, which is widely used in many aspect of life.


What is Fibonaccai Series?"

fibonacci-series-python

Fibonacci series is a series of numbers which has two first number 1 and 2. Then, the n-th element of the series is sum of 2 previous elements. For example, 3rd element is 2 which is sum of 0 and 1. Series of 0, 1, 1, 2, 3, 5, 8 is an example of fibonacci series with 7 elements.

Fibonacci Series has a lot of usages. For example, it can show ratio of natural occuring phenomenoms, such as size of swirls on snail house, and number of flower petal. Fibonacci series also used in practical field, for example in stocks price movement.

 

Fibonacci Series Algorithm

Using python, we can easily create a program that can generate Fibonacci series with N elements. But before constructing the python program, let's review fibonacci algorithm to get better understanding of the program

  1.  First, fibonacci series is started with 0 and 1. So, we must create a list with 0 and 1 already inside. 
  2. Minimum number of elements of fibonacci series is 2.
  3. If there are N elements, so new element calculation must be repeated for N-2 times, since 2 elements already presented.
  4. n-th element is sum of 2 previous elements. We can simplify this step using python indexing without specify exact position of the elements.
  5. For example, we do not need to put index of 3 and sum of 1st and 2nd element to calculate 3rd element.

That's all of the fibonacci generator algorithm. It's quite simple since fibonacci is only a series, unlike pascal triangle which is consisted of lists inside list.

Let's build python program that can generate fibonacci series with N elements.

 

Python Program of Fibonacci Series Generator

Here is full python script of Fibonacci Series Generator.

1   series = [0, 1]
2   N = 10
3   
4   for i in range(N-2):
5       element = series[-1] + series[-2]
6       series.append(element)
7   
8   print(series)

out:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 35]

The program takes input of N (number of fibonacci series element). Output of the python program is a list containing N elements of fibonacci series. Here are the explanations of the python script.

  1. First, create list that represent fibonacci series at line 1.
  2. Create variable N, which is number of fibonacci elements.
  3. Create for loop at line 4. Since the series already has 2 elements, so the loop only occur for N-2 times.
  4. For this case, we don't need to specify calculated element index. We just need to calculate new element on variable element (line 5) which is sum of 2 latest element in the series.
  5. We can use python indexing here, series[-1] and series[-2] to access  2 latest element. It's very simple since the index is not need to be specified.
  6. Calculated element area put inside the fibonacci list using method .append(). This way, new element will be always put on last index and ready to be used on calculating next element.
  7. Print the series, and finally we get fibonacci series generated using python program.  

 

Conclusion

That's all about python script to generate fibonacci series that contains N elements. Of course the script is pretty simple. There are a lot of rooms to improve it. For example, minimum N for fibonacci is 3, since 2 elements already present, we can add validation so the program only run if N > 2. I hope you enjoy it and get some inspiration from it.

Posting Komentar untuk "Creating Fibonacci Series on Python"