Skip to content

Instantly share code, notes, and snippets.

@jreighley
Last active February 19, 2019 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jreighley/74d6c3a3ac482edeea1ace3f534d7bf3 to your computer and use it in GitHub Desktop.
Save jreighley/74d6c3a3ac482edeea1ace3f534d7bf3 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes -- Clojure
(defn filter-factors [primes unfiltered]
(let [new-prime (first unfiltered)
new-primes (conj primes new-prime)
new-unfiltered (filter #(< 0 (mod % new-prime)) unfiltered)]
(if (zero? (count new-unfiltered))
new-primes
(recur new-primes new-unfiltered))))
(defn soe [n]
(filter-factors [] (range 2 (inc n))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment