Monday, June 29, 2020

Python Recursive Function for Fibonacci Sequence

def fibo(n): if n == 1 or n == 2: return 1 else: return (fibo(n - 1) + (fibo(n - 2))) print(fibo(7))



Explanation:
fibo(7)=fibo(6)+fibo(5)
fibo(7)=(fibo(5)+fibo(4))+(fibo(4)+fibo(3))
fibo(7)=(fibo(4)+fibo(3)+fibo(3)+fibo(2)+fibo(3)+fibo(2)+fibo(2)+fibo(1))
fib(7)=fibo(3)+fibo(2)+fibo(2)+fib(1)+fibo(2)+fib(1)+1+fibo(2)+fib(1)+1+1+1
fib(7)=fibo(2)+fib(1)+1+1+1+1+1+1+1+1+3
fib(7)=1+1+8+3
fib(7)=13






Flowchart: Recursion: Fibonacci sequence.

No comments:

Post a Comment