Fibonacci series in python

Fibonacci Series


Fibonacci series is 0,1,1,2,3,5,8,13,21,34,............,infinity.
It is a sequence of number which follow some type of algorithm
the alogrithm is 

let 
i = index position 
a = it is a array of number 
so to find a[i+1] = a[i]+a[i-1].

Note : fibonacci series always start with 0 and 1 and goes upto infinity.you can't start with your own values.

















Explanation:

1. 0 and 1 in starting is default values 
2. if we go further the algorithm start adding previous 2 value and the answer is the next value.
    as you can see the previous two value is 0 and 1 after adding both we get 1 as our 3rd value.
    after that adding 1 and 1 we get our 4th value as 2 this entire cycle goes like this only.


i think you get better understanding about fibonacci series we move further on coding of fibo 
in python:

Flowchart:


coding:

without recursion function:

def fibonacci(n):
    a = 0
    b = 1
    if n <= 0:
        print("Incorrect input")
    else:
        print(a,"\n",b)
        for i in range(2,n):
            c = a + b
            print(c)
            a = b
            b = c
        return b
 
fibonacci(9)

output:

0
1
1
2
3
5
8
13
21


Explanation :
in the code we pass 9 as a last index number of Fibonacci it mean we just
won't 9 numbers only that.

as condition that 0 and 1 is the default starting two value we pass to A and B.
after that we check conditions

1.check weather the value pass by user is less that 0(-ve) than return
incorrect input.
2.in else part we start our loop we first print A and B value then the loop
  is started with the range from 2,....,N.
  in loop we do C=A+B because in Starting 2 value is A=0 and B=1 .
we simply add A and B and store it in C variable.

after that we print C and then we swap the value A=B and B=C .
this is until for loop condition gets false.


hello guys hope you like this post please stay tuned for regular post
i am here to explain you every concept about python so please help us to reach each one who want to learn python.

please share and appreciate



Comments

Post a Comment

Popular posts from this blog

Python Class and Objects

exception handling in python