-
-
Save kindlychung/6c690044dcbbba0c464e to your computer and use it in GitHub Desktop.
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
;; 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