Skip to content

Instantly share code, notes, and snippets.

@paultopia
Last active December 8, 2016 05:10
Show Gist options
  • Select an option

  • Save paultopia/e102a3b16fd550fec3bd9ea1528b3d3a to your computer and use it in GitHub Desktop.

Select an option

Save paultopia/e102a3b16fd550fec3bd9ea1528b3d3a to your computer and use it in GitHub Desktop.
euler problem 2 (sum even fib sequence < 4m) in clj/cljs
;; note: the extra case is to fit weird euler def. of fib seq that goes 1, 2, 3, 5, 8, 13, ... rather than 1, 1, 2, 3, 5, 8, 13
;; not nearly as cool as the fibs here: https://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci
(def fib
(memoize
(fn [n]
(case n
0 0
1 1
2 2
(+ (fib (- n 1)) (fib (- n 2)))))))
(def xf-fib
(comp
(map fib)
(take-while #(<= % 4000000))
(filter even?)))
(print (transduce xf-fib + (range)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment