Last active
October 30, 2015 00:12
-
-
Save tabdelmaguid/f6582065216d6a170434 to your computer and use it in GitHub Desktop.
Primes Lazy Sequence in Clojure
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 divisible-by? [n factor] | |
(= (mod n factor) 0)) | |
(defn primes [] | |
(letfn | |
[(next-primes [rest-of-the-numbers] | |
(let [[prime & rest] rest-of-the-numbers] | |
(cons prime | |
(lazy-seq | |
(next-primes (filter #(not (divisible-by? % prime)) rest))))))] | |
(next-primes (iterate inc 2)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Produces
StackOverflowError
for large primes