Skip to content

Instantly share code, notes, and snippets.

@tabdelmaguid
Last active October 30, 2015 00:12
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 tabdelmaguid/f6582065216d6a170434 to your computer and use it in GitHub Desktop.
Save tabdelmaguid/f6582065216d6a170434 to your computer and use it in GitHub Desktop.
Primes Lazy Sequence in Clojure
(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))))
@tabdelmaguid
Copy link
Author

Produces StackOverflowError for large primes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment