Skip to content

Instantly share code, notes, and snippets.

@msgodf
Created May 20, 2013 12:03
Show Gist options
  • Save msgodf/5611845 to your computer and use it in GitHub Desktop.
Save msgodf/5611845 to your computer and use it in GitHub Desktop.
Concurrent prime number sieve. I wanted something that took a while, so that the agent could be running in the background for some time. I used an atom so that I could monitor the sieve externally.
(def sieve (atom []))
(def siever (agent sieve))
(send-off siever
(fn [x test-set]
(doall
(map (fn [n]
(if (not (some
#(zero? (mod n %))
@x))
(swap! x (partial cons n))))
test-set))
x)
(range 2 30000))
(dotimes [n 100] ((prn (count @sieve)) (Thread/sleep 500)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment