Skip to content

Instantly share code, notes, and snippets.

@stuartstein777
Created February 24, 2021 21:19
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 stuartstein777/c33b8468e40a6f4e44d967a7c28ae832 to your computer and use it in GitHub Desktop.
Save stuartstein777/c33b8468e40a6f4e44d967a7c28ae832 to your computer and use it in GitHub Desktop.
sieve of erastothanes
;; slow :(
(defn sieve-primes [upper]
(let [xs (->> (range 1 (inc upper))
(set))]
(sort (reduce #(if (or (= 1 %2) (not (%1 %2)))
%1
(let [multiples (range (* 2 %2) (inc upper) %2)]
(set/difference %1 multiples))) xs (range 1 (Math/sqrt (inc upper)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment