-
-
Save juergenhoetzel/964676 to your computer and use it in GitHub Desktop.
sieve.clj
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
(set! *unchecked-math* true) | |
(defmacro iloop [[b t n] & body] | |
`(loop [~@b] | |
(when ~t | |
~@body | |
(recur ~n)))) | |
(defn count-primes [^long n] | |
(let [c (inc n) | |
^java.util.BitSet prime? (java.util.BitSet. c)] | |
(.set prime? 2 n) | |
(iloop [(i 2) (<= (* i i) n) (inc i)] | |
(if (.get prime? i) | |
(iloop [(j i) (<= (* i j) n) (inc j)] | |
(.clear prime? (* i j))))) | |
(.cardinality prime?))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice :D