Last active
December 8, 2016 05:10
-
-
Save paultopia/e102a3b16fd550fec3bd9ea1528b3d3a to your computer and use it in GitHub Desktop.
euler problem 2 (sum even fib sequence < 4m) in clj/cljs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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