Skip to content

Instantly share code, notes, and snippets.

@pbostrom
Created July 9, 2012 02:39
Show Gist options
  • Save pbostrom/3073902 to your computer and use it in GitHub Desktop.
Save pbostrom/3073902 to your computer and use it in GitHub Desktop.
Clojure Sieve of Eratosthenes - Christophe Grand
(defn primes3 [max]
(let [enqueue (fn [sieve n factor]
(let [m (+ n (+ factor factor))]
(if (sieve m)
(recur sieve m factor)
(assoc sieve m factor))))
next-sieve (fn [sieve candidate]
(if-let [factor (sieve candidate)]
(-> sieve
(dissoc candidate)
(enqueue candidate factor))
(enqueue sieve candidate candidate)))]
(cons 2 (vals (reduce next-sieve {} (range 3 max 2))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment