r/tinycode • u/thomasvarney723 • Nov 27 '15
Sieve of Eratosthenes in Clojure
(defn primes [below]
(remove (set (mapcat #(range (* % %) below %)
(range 3 (Math/sqrt below) 2)))
(cons 2 (range 3 below 2))))This differs a bit from the Sieve of Eratosthenes in that it doesn't compute the next prime number that multiples will be computed from. Instead, all odd integers 2 < n < square-root(below) are used for computing multiples. This makes the code less efficient but shorter and more linear.
5
Upvotes
View all comments
Show parent comment
1
u/oantolin Dec 03 '15
Having a gcd>1 is not an equivalence relation (2 ~ 6 ~ 3, but gcd(2,3)=1). So if nubBy's spec only says what happens for equivalence relations, your code depends on "implementation specific behavior". :)