The lisp-way to solve Fibonnaci

前端 未结 14 1680
别那么骄傲
别那么骄傲 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 08:01

    (defun fib (x &optional (y 0) (z 1))
               (if (< x z)
                   nil
                   (append (list z) (fib x z (+ y z)))))
    
    CL-USER> (reduce #'+ (remove-if-not #'evenp (fib 1000000)))
    

提交回复
热议问题