Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(set! *unchecked-math* true)
(defmacro iloop [[b t n] & body]
`(loop [~@b]
(when ~t
(recur ~n))))
(defn count-primes [^long n]
(let [c (inc n)
^booleans prime? (make-array Boolean/TYPE c)]
(iloop [(i 2) (<= i n) (inc i)]
(aset prime? i true))
(iloop [(i 2) (<= (* i i) n) (inc i)]
(if (aget prime? i)
(iloop [(j i) (<= (* i j) n) (inc j)]
(aset prime? (* i j) false))))
(areduce prime? i r 0
(if (aget prime? i)
(inc r)
Copy link

swannodette commented May 9, 2011

heh, yeah thanks.

Copy link

ejconlon commented May 10, 2011

Thanks for the great post. I can follow everything but the "[Z" bit... ^"[Z" assigns the metadata {:tag "[Z"} to prime?, correct? What does the compiler do with that metadata?

Copy link

juergenhoetzel commented May 10, 2011

Nice post!

The use of Java BitSets can further improve performance (30 % on my system):

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