Time Complexity of Fibonacci Algorithm?

Your recursive code has exponential runtime. But I don't think the base is 2, but probably the golden ratio (about 1.62). But of course O(1.62^n) is automatically O(2^n) too.

Your recursive code has exponential runtime. But I don't think the base is 2, but probably the golden ratio (about 1.62). But of course O(1.62^n) is automatically O(2^n) too.

The runtime can be calculated recursively: t(1)=1 t(2)=1 t(n)=t(n-1)+t(n-2)+1 This is very similar to the recursive definition of the fibonacci numbers themselves. The +1 in the recursive equation is probably irrelevant for large n. S I believe that it grows approximately as fast as the fibo numbers, and those grow exponentially with the golden ratio as base.

You can speed it up using memoization, i.e. Caching already calculated results. Then it has O(n) runtime just like the iterative version.

Your iterative code has a runtime of O(n) You have a simple loop with O(n) steps and constant time for each iteration.

Ahhh, sorry, I posted the wrong code. I needed the recursive one! My bad, will edit in a sec.

– Koeneuze Jan 22 at 15:48 O(ð? Œ™ ^ n) = O(2 ^ n). – larsmans Jan 22 at 16:00 @lars That's why I wrote "But of course O(1.62) is automatically O(2) too".

But it's still interesting to know a closer upper bound than 2^n. – CodeInChaos Jan 22 at 16:02 O(ð? Œ™ ^ n) = O(2 ^ n) does not directly follow from O(ð?

Œ™) = O(2), since O(n^ð? Œ™)! = O(n²).

I don't this particularly interesting, as there's an O(lg n) (O(n) in the number of bits) algorithm for this problem. – larsmans Jan 22 at 16:10 1 @lars sorry it was a typo. I wanted to write O(1.62^n) is automatically O(2^n) but forgot the ^n – CodeInChaos Jan 22 at 16:14.

You can use this to calculate Fn in O(log n).

Each function call does exactly one addition, or returns 1. The base cases only return the value one, so the total number of additions is fib(n)-1. The total number of function calls is therefore 2*fib(n)-1, so the time complexity is Θ(fib(N)) = Θ(phi^N), which is bounded by O(2^N).

It is linear in time so it is O(n). You have a for loop running O(n) times and doing O(1) operations in each iteration.

Sorry, had pasted wrong code, my bad. – Koeneuze Jan 22 at 15:50.

I see only O(n) here. I wonder why you'd continue to calculate and re-calculate these? Wouldn't caching the ones you have be a good idea, as long as the memory requirements didn't become too odious?

Since they aren't changing, I'd generate a table and do lookups if speed mattered to me.

It's easy to see (and to prove by induction) that the total number of calls to fibonacciRecursive is exactly equal to the final value returned. That is indeed exponential in the input number.

No it's slightly higher. For example Fibo(2) has 3 calls, but returns 2. But for large n the difference doesn't matter much.

– CodeInChaos Jan 22 at 16:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions