Last active
September 16, 2015 08:46
-
-
Save p-baleine/e709b20a0b52c58aa41c to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Clojur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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