Skip to content

Instantly share code, notes, and snippets.

@sleepynate
Created August 31, 2010 20:47
Show Gist options
  • Save sleepynate/559723 to your computer and use it in GitHub Desktop.
Save sleepynate/559723 to your computer and use it in GitHub Desktop.
; Project Euler Problem 2
; Solution by nathan dotz - nathan (period) dotz (at sign) gmail (period) com
;
; Each new term in the Fibonacci sequence is generated by adding
; the previous two terms. By starting with 1 and 2, the first
; 10 terms will be:
; 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
;
; Find the sum of all the even-valued terms in the sequence
; which do not exceed four million.
;
(ns euler2)
(def fib-seq
"Generate a lazy fibonacci sequence with corecursion"
(lazy-cat [0 1] (map + (rest fib-seq) fib-seq))
)
(def small-fib
"Sequence of fibonacci below 4 million"
(take-while #(<= % 4000000) fib-seq)
)
(def even-small-fib
"All fibonacci nums below 4 million and even"
(filter #(even? %) small-fib)
)
(print
(apply + even-small-fib)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment