Skip to content

Instantly share code, notes, and snippets.

@Ramasubramanian
Created July 24, 2011 16:13
Show Gist options
  • Save Ramasubramanian/1102776 to your computer and use it in GitHub Desktop.
Save Ramasubramanian/1102776 to your computer and use it in GitHub Desktop.
Euler problem 10 in Clojure
(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