Fast Fibonacci computation

前端 未结 4 1797
失恋的感觉
失恋的感觉 2021-02-04 17:56

I saw a comment on Google+ a few weeks ago in which someone demonstrated a straight-forward computation of Fibonacci numbers which was not based on recursion and didn\'t use mem

4条回答
  •  滥情空心
    2021-02-04 18:54

    This is too long for a comment, so I'll leave an answer.

    The answer by Aaron is correct, and I've upvoted it, as should you. I will provide the same answer, and explain why it is not only correct, but the best answer posted so far. The formula we're discussing is:

    Computing Φ is O(M(n)), where M(n) is the complexity of multiplication (currently a little over linearithmic) and n is the number of bits.

    Then there's a power function, which can be expressed as a log (O(M(n)•log(n)), a multiply (O(M(n))), and an exp (O(M(n)•log(n)).

    Then there's a square root (O(M(n))), a division (O(M(n))), and a final round (O(n)).

    This makes this answer something like O(n•log^2(n)•log(log(n))) for n bits.


    I haven't thoroughly analyzed the division algorithm, but if I'm reading this right, each bit might need a recursion (you need to divide the number log(2^n)=n times) and each recursion needs a multiply. Therefore it can't be better than O(M(n)•n), and that's exponentially worse.

提交回复
热议问题