Created
March 16, 2011 02:31
-
-
Save mecdemort/871908 to your computer and use it in GitHub Desktop.
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
(use '[clojure.contrib.lazy-seqs :only [primes]] | |
'[clojure.set]) | |
(def prime? | |
(let [mem (ref #{}) | |
primes (ref primes)] | |
(fn [n] | |
(if (< n (first @primes)) | |
(@mem n) | |
(let [[mems ps] (split-with #(<= % n) @primes)] | |
(dosync | |
(ref-set primes ps) | |
((commute mem union (set mems)) n))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment