Skip to content

Instantly share code, notes, and snippets.

@p-baleine
Last active September 16, 2015 08:46
Show Gist options
  • Save p-baleine/e709b20a0b52c58aa41c to your computer and use it in GitHub Desktop.
Save p-baleine/e709b20a0b52c58aa41c to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Clojur
(defn eratosthenes
"finding all prime numbers up to `x`"
[x]
(letfn [(continue? [x list]
(< (first list) (Math/sqrt x)))
(go [x list primes]
(if (continue? x list)
(let [head (first list)
new-list (filter #(not (= (mod %1 head) 0)) (rest list))]
(recur x new-list (cons head primes)))
(concat primes list)))]
(go x (range 2 x) '())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment