Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Created April 17, 2015 16:35
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 kindlychung/6c690044dcbbba0c464e to your computer and use it in GitHub Desktop.
Save kindlychung/6c690044dcbbba0c464e to your computer and use it in GitHub Desktop.
;; Helper function for marking multiples of a number as 0
(def mark (fn [[x & xs] k m]
(if (= k m)
(cons 0 (lazy-seq (mark xs 1 m)))
(cons x (lazy-seq (mark xs (inc k) m)))
)))
;; Sieve of Eratosthenes
(defn sieve
[x & xs]
(if (= x 0)
(lazy-seq (sieve xs))
(cons x (lazy-seq (sieve (mark xs 1 x))))
))
(take 10 (sieve (iterate inc 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment