Skip to content

Instantly share code, notes, and snippets.

@knjname
Created January 2, 2014 17:29
Show Gist options
  • Select an option

  • Save knjname/8222787 to your computer and use it in GitHub Desktop.

Select an option

Save knjname/8222787 to your computer and use it in GitHub Desktop.
;; http://projecteuler.net/problem=2
(def fib
(memoize
(fn [n]
(cond
(= n 0) 1
(= n 1) 2
true (+ (fib (- n 1)) (fib (- n 2)))))))
(defn lazy-seq-fibo
([]
(concat [0 1] (lazy-seq-fibo 0N 1N)))
([a b]
(let [n (+ a b)]
(lazy-seq
(cons n (lazy-seq-fibo b n))))))
(defn ignore-head [n col]
(if (= n 0)
col
(recur (- n 1) (rest col))))
(println (->> (lazy-seq-fibo)
(ignore-head 2)
(filter even?)
(take-while (fn [n] (<= n 4000000N)))
(reduce + 0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment