Computational complexity of Fibonacci Sequence

后端 未结 11 1448
慢半拍i
慢半拍i 2020-11-21 10:27

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

11条回答
  •  抹茶落季
    2020-11-21 10:33

    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.

提交回复
热议问题