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 sieve [[nums primes]] | |
(let [[prime & more] nums] | |
(vector (remove #(zero? (rem % prime)) nums) | |
(cons prime primes)))) | |
(defn primes-below [n] | |
(let [sqrt-n (Math/sqrt n)] | |
(apply | |
concat | |
(first | |
(drop-while | |
#(< (ffirst %) sqrt-n) | |
(iterate sieve [(range 2 (inc n)) nil])))))) | |
(defn sum-primes-below [n] | |
(reduce + (primes-below n))) | |
(sum-primes-below 10) ; 17 | |
(sum-primes-below 100) ; 1060 | |
(sum-primes-below 1000) ; 76127 | |
(sum-primes-below 10000) ; 5736396 | |
(sum-primes-below 100000) ; 454396537 | |
(sum-primes-below 2000000) ; 142913828922 |
No comments:
Post a Comment