The lisp-way to solve Fibonnaci

前端 未结 14 1474
别那么骄傲
别那么骄傲 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:05

    The way to solve this is to work bottom-up, generating each Fibonnaci term one-by-one, and adding it to the sum if it's even, and stopping once we reach the 4 million threshold. My LISP is rusty, so here it is in psuedocode:

    one_prior = 1
    two_prior = 1
    curr = 2
    sum = 0
    while curr < 4000000000
      if curr % 2 == 0
        sum = sum + curr
      two_prior = one_prior
      one_prior = curr
      curr = one_prior + two_prior
    

提交回复
热议问题