Created
July 9, 2012 02:39
-
-
Save pbostrom/3073902 to your computer and use it in GitHub Desktop.
Clojure Sieve of Eratosthenes - Christophe Grand
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 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
See http://clj-me.cgrand.net/index.php?s=Everybody%20loves%20the%20Sieve%20of%20Eratosthenes