# Time Complexity of Fibonacci Algorithm

So, i've got a recursive method in Java for getting the 'n'th fibonacci number - The only question i have, is: what's the time complexity? I think it's O(2^n), but i may be mistaken? (I know that iterative is way better, but it's an exercise)

``````public int fibonacciRecursive(int n)
{
if(n == 1 || n == 2) return 1;
else return fibonacciRecursive(n-2) + fibonacciRecursive(n-1);
}
``````
-
possible duplicate of Computational complexity of Fibonacci Sequence –  codaddict Jan 22 '11 at 15:52
“Iterative is way better” is wrong in general. It’s true that it beats your naive recursive implementation but that implementation can be improved to be essentially on par with an iterative implementation using dynamic programming. –  Konrad Rudolph Jan 22 '11 at 16:29

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 '11 at 15:48
O(𝜙 ^ n) = O(2 ^ n). –  larsmans Jan 22 '11 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. –  CodesInChaos Jan 22 '11 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 '11 at 16:10
@lars sorry it was a typo. I wanted to write `O(1.62^n)` is automatically `O(2^n)` but forgot the `^n` –  CodesInChaos Jan 22 '11 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).

-

O(2^n)? 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. –  CodesInChaos Jan 22 '11 at 16:04
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://stackoverflow.com/questions/4768781/time-complexity-of-fibonacci-algorithm", "fetch_time": 1438139573000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2015-32/segments/1438042985647.51/warc/CC-MAIN-20150728002305-00082-ip-10-236-191-2.ec2.internal.warc.gz", "warc_record_offset": 227228153, "warc_record_length": 19376, "token_count": 935, "char_count": 3355, "score": 3.6875, "int_score": 4, "crawl": "CC-MAIN-2015-32", "snapshot_type": "latest", "language": "en", "language_score": 0.8887478709220886, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00014-of-00064.parquet"}