Skip to content

Instantly share code, notes, and snippets.

@juergenhoetzel
Forked from swannodette/gist:962877
Created May 10, 2011 15:20
Show Gist options
  • Save juergenhoetzel/964676 to your computer and use it in GitHub Desktop.
Save juergenhoetzel/964676 to your computer and use it in GitHub Desktop.
sieve.clj
(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?)))
@swannodette
Copy link

Nice :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment