I am currently learning Recursion with Micheal.T.Goodrich's "Algorithm and DataStructure with Python". The author suggests a better approach to computing the nth Fibonacci number using recursion without the space complexity of the usual approach. But I just can't wrap my head around the intuition of the program. Can someone explain this:
- Normal Recursive function to find nth Fibonacci number
def Fibonacci(n):
if n < = 1:
return (n)
else:
return Fibonacci(n - 1) + Fibonacci (n - 2)
- Optimized solution:
def Fibonnaci(n):
if n <= 1:
return (n, 0)
else:
(a, b) = Fibonacci(n - 1)
return (a+b, a)
Can someone help me in understanding the optimized solution?