Fibonacci Series

In mathematical terms, the sequence Fn of Fibonacci numbers
is defined by the recurrence relation.

Fn = Fn-1 + Fn-2
with seed values :
F0 = 0 and F1 = 1.


***The Fibonacci numbers are the numbers in
 the following integer sequence:
-->0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,....


Python program(using recursive function):

def nth_fibonacci(n):
    if n<0:
        return "Invalid"
    if n==0:
        return 0
    elif n==1:
        return 1
    else:
        return nth_fibonacci(n-1)+nth_fibonacci(n-2)

print(nth_fibonacci(9))     


Another approach(Using formula) :
In this method we directly implement the
formula for nth term in the fibonacci series. 
Fn = {[(√5 + 1)/2] ^ n} / √5

import math
def fibo(n):
    phi = (1 + math.sqrt(5)) / 2

    return round(pow(phi, n) / math.sqrt(5))
    
# Driver code
if __name__ == '__main__':
    n = 9   
    print(fibo(n))










Comments

Popular Posts