I understand Big-O notation, but I don\'t know how to calculate it for many functions. In particular, I\'ve been trying to figure out the computational complexity of the nai
Just ask yourself how many statements need to execute for F(n)
to complete.
For F(1)
, the answer is 1
(the first part of the conditional).
For F(n)
, the answer is F(n-1) + F(n-2)
.
So what function satisfies these rules? Try an (a > 1):
an == a(n-1) + a(n-2)
Divide through by a(n-2):
a2 == a + 1
Solve for a
and you get (1+sqrt(5))/2 = 1.6180339887
, otherwise known as the golden ratio.
So it takes exponential time.