Fibonacci series in Python and Fibonacci Number Program

What is the Fibonacci Series?

According to Google Fibonacci Series is a series of numbers

in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.

The Fibonacci Sequence is the series of numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

The next number is found by adding up the two numbers before it.

  • The 2 is found by adding the two numbers before it (1+1)
  • The 3 is found by adding the two numbers before it (1+2),
  • And the 5 is (2+3),
  • and so on!

It is that simple!

Here is a longer list:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, …

Can you figure out the next few numbers?

History

The Fibonacci sequence appears in Indian mathematics in connection with Sanskrit prosody, as pointed out by Parmanand Singh in 1985.[8][10][11] In the Sanskrit poetic tradition, there was interest in enumerating all patterns of long (L) syllables of 2 units duration, juxtaposed with short (S) syllables of 1 unit duration. Counting the different patterns of successive L and S with a given total duration results in the Fibonacci numbers: the number of patterns of duration m units is Fm + 1.[9]

Knowledge of the Fibonacci sequence was expressed as early as Pingala (c. 450 BC–200 BC). Singh cites Pingala’s cryptic formula misrau cha (“the two are mixed”) and scholars who interpret it in context as saying that the number of patterns for m beats (Fm+1) is obtained by adding one [S] to the Fm cases and one [L] to the Fm−1 cases.[12] Bharata Muni also expresses knowledge of the sequence in the Natya Shastra (c. 100 BC–c. 350 AD).[13][7] However, the clearest exposition of the sequence arises in the work of Virahanka (c. 700 AD), whose own work is lost, but is available in a quotation by Gopala (c. 1135)

Leonardo Pisano Bogollo Fibonacci Man

About Fibonacci The Man

His real name was Leonardo Pisano Bogollo, and he lived between 1170 and 1250 in Italy.

“Fibonacci” was his nickname, which roughly means “Son of Bonacci”.

As well as being famous for the Fibonacci Sequence, he helped spread Hindu-Arabic Numerals (like our present numbers 0,1,2,3,4,5,6,7,8,9) through Europe in place of Roman Numerals (I, II, III, IV, V, etc). That has saved us all a lot of trouble! Thank you, Leonardo.

Also Read: How Instagram Is Using Django And Python

Fibonacci Day

Fibonacci Day is November 23rd, as it has the digits “1, 1, 2, 3” which is part of the sequence. So next Nov 23 let everyone know!

The Rule for Fibonacci Series

The Fibonacci Sequence can be written as a “Rule”

First, the terms are numbered from 0 onwards like this:

n =01234567891011121314
xn =01123581321345589144233377

So term number 6 is called x6 (which equals 8).

Example: the 8th term is
the 7th term plus the 6th term:
x8 = x7 + x6

So we can write the rule:

The Rule is xn = xn-1 + xn-2

where:

  • Here xn is term number “n”
  • xn-1 is the previous term (n-1)
  • And xn-2 is the term before that (n-2)

Python Program for Fibonacci Series/ Sequence

Python Program for Fibonacci Series using Iterative Approach

This approach is based on the following algorithm
1. Declare two variables representing two terms of the series. Initialize them to 0 and 1 as the first and second terms of the series respectively.
2. Initialize a variable representing loop counter to 0.
3. Loop from 0 to the total number of terms in the series.
4. In every iteration,
A. add the variables defined in step 1. This represents a term(or item) of the Fibonacci series.
B. assign the value of the second variable to first and the sum in above step A to the second variable.
So Python program to generate Fibonacci series written as per the above algorithm follows.

def fibonacci(num):
    num1 = 0
    num2 = 1
    series = 0
    for i in range(num):
        print(series, end=' ');
        num1 = num2;
        num2 = series;
        series = num1 + num2;


# running function after takking user input
num = int(input('Enter how many numbers needed in Fibonacci series- '))
fibonacci(num)

Thus the Output of the Execution is:

Enter how many numbers needed in Fibonacci series
6
0,1,1,2,3,5,

Python Program for Fibonacci Series using recursion

Create a recursive function which receives an integer as an argument. This integer argument represents the position in Fibonacci series and returns the value at that position. Thus, if it receives 5, it returns the value at 5th position in Fibonacci series.
This recursive function returns 0 and 1 if the argument value is 0 or 1. For all other values, it calls itself with the sum of nth and (n-1)th positions.
The program reads the total number of elements in Fibonacci series from the keyboard. It then initiates a loop starting from 0 till this input value. In every iteration, the recursive function is called and the resultant Fibonacci item for that position is printed.

def fibonacci(number):
    # return 0 and 1 for first and second terms
    if number == 0:
        return 0
    elif number == 1:
        return 1
    else:
        # return the sum of two numbers
        return fibonacci(number - 1) + fibonacci(number - 2)
 
# read the total number of items in Fibonacci series
max_item_input = input("Enter the number of items in Fibonacci series\n")
max_item = int(max_item_input)
 
# iterate from 0 till number of terms
for count in range(max_item):
    print(fibonacci(count), end=",")

Thus the output of the above execution is

Enter the number of items in Fibonacci series
8
0,1,1,2,3,5,8,13,

Applications of Fibonacci Series / Sequence / Number

  • First of all the Fibonacci numbers are important in the computational run-time analysis of Euclid’s algorithm to determine the greatest common divisor of two integers: the worst case input for this algorithm is a pair of consecutive Fibonacci numbers.
  • The Fibonacci numbers are also an example of a complete sequence. So, this means that every positive integer can be written as a sum of Fibonacci numbers, where anyone number is used once at most.
  • Fibonacci numbers are used by some pseudorandom number generators.
  • They are also used in planning poker, which is a step in estimating software development projects that use the Scrum methodology.
  • Also, Fibonacci numbers arise in the analysis of the Fibonacci heap data structure.
  • Retracement of Fibonacci levels is widely used in technical analysis for financial market trading.

So, I hope you liked this article and if you have any questions/recommendations or just want to say hi, comment below!

Leave a Reply