even_fib_terms.gsp 520 B

12345678910111213141516171819
  1. (ns main
  2. "fmt"
  3. "github.com/jcla1/gisp/core")
  4. (def main (fn []
  5. (let [[n 4000000]]
  6. (fmt/printf "Sum of all even fibonacci terms below %d: %0.0f\n" n (sum-even-fib n))
  7. ())))
  8. (def sum-even-fib (fn [not-exceeding]
  9. (loop [[a 0.0]
  10. [b 1.0]
  11. [sum 0.0]]
  12. (let [[next (+ a b)]]
  13. (if (>= next not-exceeding)
  14. sum
  15. (if (= 0 (mod next 2))
  16. (recur b next (+ sum next))
  17. (recur b next sum)))))))