Time complexity of recursive Fibonacci program
The Fibonacci numbers are the numbers in the following integer sequence 0, 1, 1, 2, 3, 5, 8, 13…
Mathematically Fibonacci numbers can be written by the following recursive formula.
For seed values F(0) = 0 and F(1) = 1 F(n) = F(n-1) + F(n-2)
Before proceeding with this article make sure you are familiar with the recursive approach discussed in Program for Fibonacci numbers
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Analysis of the recursive Fibonacci program:
We know that the recursive equation for Fibonacci is =++.
What this means is, the time taken to calculate fib(n) is equal to the sum of time taken to calculate fib(n-1) and fib(n-2). This also includes the constant time to perform the previous addition.
On solving the above recursive equation we get the upper bound of Fibonacci as but this is not the tight upper bound. The fact that Fibonacci can be mathematically represented as a linear recursive function can be used to find the tight upper bound.
Now Fibonacci is defined as
The characteristic equation for this function will be
– – =
Solving this by quadratic formula we can get the roots as
= (+)/ and =( – )/
Now we know that solution of a linear recursive function is given as
where and are the roots of the characteristic equation.
So for our Fibonacci function = + the solution will be
Clearly and are asymptotically the same as both functions are representing the same thing.
Hence it can be said that
or we can write below (using the property of Big O notation that we can drop lower order terms)
This is the tight upper bound of fibonacci.\
1.6180 is also called the golden ratio. You can read more about golden ratio here: Golden Ratio in Maths
This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.