The lisp-way to solve Fibonnaci

前端 未结 14 1657
别那么骄傲
别那么骄傲 2021-02-08 07:17

I wanted to try and learn Lisp, but I very quickly gave up. I figured I\'d try again. I\'m looking at Problem 2 on Project Euler - finding the sum of all even Fibonacci numbers

14条回答
  •  無奈伤痛
    2021-02-08 07:47

    In addition to all useful answers, the following formulas may provide even more efficiency -- calculating Fn in O(Log(n)) instead of O(2^n). This has to be coupled with memoization, and is a solid base for solving the problem:

    F(2*n) = F(n)^2 + F(n-1)^2
    
    F(2*n + 1) = ( 2*F(n-1) + F(n) )   *   F(n)
    

提交回复
热议问题