Created
January 24, 2022 18:33
-
-
Save danownsthisspace/4f0679574de1124762224088c31bd3b3 to your computer and use it in GitHub Desktop.
PRIME SIEVE ALGORITHM ( SIEVE OF ERATOSTHENES )
This file contains hidden or 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 filter-multiple [n v] | |
(if (= 1 n) v | |
(filter (fn [x] | |
(or | |
(= x n) | |
(not= 0 (mod x n)))) v))) | |
(defn sieve [input] | |
(let [limit (Math/sqrt input) | |
numbers (range 2 input)] | |
(reduce (fn [acc v] | |
(if (>= v limit) | |
acc | |
(filter-multiple v acc))) numbers numbers))) |
This file contains hidden or 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 [input] | |
(let [numbers (set (range 2 (inc input))) | |
limit (Math/sqrt input)] | |
(reduce (fn [acc n] | |
(if (> n limit) | |
acc | |
(let [multiples (set (range (* 2 n) (inc input) n))] | |
(clojure.set/difference acc multiples)))) numbers numbers))) | |
(comment | |
(clojure.set/difference #{3 6 9} (set (range (* 2 3) 10 3))) | |
(time (count (sieve 1000000))) | |
(clojure.set/difference (set (range 2 100 1)) (set (range 2 100 2))) | |
(filter (set (range 2 100 1)) [1 2 3 4 5 6 7]) | |
(set (range 2 100 2)) | |
(filter-multiples 3 [3 6 9 10]) | |
(mod 10 3) | |
(inc 5) | |
(range 0 5) | |
(sieve 100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment