Created
July 24, 2011 16:13
-
-
Save Ramasubramanian/1102776 to your computer and use it in GitHub Desktop.
Euler problem 10 in Clojure
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
(defn sieve-of-era | |
"Generate the primes upto N based on sieve of erastothenes" | |
([num] (sieve-of-era [] num (range 2 num))) | |
([primes num sieve] | |
(let [x (first sieve)] | |
(def divisible? (fn [divisor num] (zero? (rem num divisor)))) | |
(def remaining (fn [coll x] (remove (partial divisible? x) coll))) | |
;optimisation - enough to remove until current prime ^ 2 < limit | |
(if (>= (* x x) num) (concat primes sieve) | |
(recur (conj primes x) num (remaining (rest sieve) x)))))) | |
(defn euler-10 [num] | |
(reduce + (sieve-of-era num))) | |
(time (println (euler-10 2000000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment