V-Lab @ ANDC

To generate a Fibonacci series

Aim

To generate a Fibonacci series till a given iteration

Theory

In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1, although some authors omit the initial terms and start the sequence from 1 and 1 or from 1 and 2. Starting from 0 and 1, the next few values in the sequence are

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

We find applications of the Fibonacci series around us in our day-to-day lives. It is also found in biological settings, like in the branching of trees, patterns of petals in flowers, etc. Let us understand the Fibonacci series formula, properties, and its applications in the following sections.

The Fibonacci series is the sequence of numbers (also called Fibonacci numbers), where every number is the sum of the preceding two numbers, such that the first two terms are '0' and '1'. In some older versions of the series, the term '0' might be omitted. A Fibonacci series can thus be given as, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... It can be thus be observed that every term can be calculated by adding the two terms before it.

Procedure

    The Fibonacci series formula in maths can be used to find the missing terms in a Fibonacci series. The formula to find the (n+1)th term in the sequence is defined using the recursive formula, such that F0 = 0, F1 = 1 to give Fn.

    The Fibonacci formula is given as follows.
    Fn = Fn-1 + Fn-2, where n > 1

    1. F0= 0, F1=1
    2. Fn= Fn-1 + Fn-2
    3. for n>1
    4. Fibonacci series: 0,1,1,2,3,5,8,13,...

Practice

Enter the number till which you want the Fibonacci series:

Here's the result




Python Code

                        
                            # Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
   print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
# generate fibonacci sequence
else:
   print("Fibonacci sequence:")
   while count < nterms:
       print(n1)
       nth = n1 + n2
       # update values
       n1 = n2
       n2 = nth
       count += 1

                        
                    

Result

Hence we can find Fibonacci series till nth iteration.