Created
July 10, 2011 02:03
-
-
Save bdesham/1074160 to your computer and use it in GitHub Desktop.
Clojure implementation of Project Euler problem 12
This file contains 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
;; Utility functions that will be used later | |
(def naturals (iterate inc 1)) | |
(defn all-factors | |
[n] | |
(filter #(= (mod n %) 0) | |
(take-while #(<= % (sqrt n)) | |
(rest naturals)))) | |
(defn first-matching | |
"Given a function and a sequence, return the first element of the sequence for | |
which the function returns true." | |
[f s] | |
(if (f (first s)) | |
(first s) | |
(if (seq (rest s)) | |
(recur f (rest s)) | |
nil))) | |
;; Project Euler problem 12 | |
(def triangle-numbers (map #(reduce + (take % naturals)) naturals)) | |
(def problem12 | |
(delay (first-matching #(> (count (all-factors #)) 500) triangle-numbers))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment