Created
July 18, 2010 21:24
-
-
Save elben/480723 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
(def seen-primes (atom [])) | |
;; BUG: if composite c is larger than any p in known-primes-atom, | |
;; and (rem c p) != 0 for all p, then c would be seen as a prime, though | |
;; it is a composite number. | |
(defn memoized-prime? [known-primes-atom n] | |
(if (< n 2) | |
false | |
(every? #(not (zero? (rem n %))) | |
(for [d @known-primes-atom :when (< (* d d) n)] d)))) | |
(defn memoizing-prime? [n] | |
(if (memoized-prime? seen-primes n) | |
(do | |
(swap! seen-primes conj n) | |
true) | |
false)) | |
(defn memoized-sum-primes [mx] | |
(reduce + (for [x (range (inc mx)) :when (memoizing-prime? x)] x))) | |
(println (memoized-sum-primes 100)) | |
(println (memoized-sum-primes 1000)) | |
(println (memoized-sum-primes 10000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment