Created
August 28, 2011 01:40
-
-
Save Netpilgrim/1176126 to your computer and use it in GitHub Desktop.
Lazy seq of primes
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 mult? | |
"Returns true iff n is a multiple of one of the numbers in coll." | |
[coll n] | |
(some #(zero? (rem n %)) coll)) | |
(def primes | |
(letfn [(rest-primes | |
[known-primes] | |
(lazy-seq | |
(let [highest-known-prime (peek known-primes), | |
next-prime | |
(first (drop-while | |
#(mult? known-primes %) | |
(iterate inc (inc highest-known-prime))))] | |
(cons highest-known-prime | |
(rest-primes (conj known-primes next-prime))))))] | |
(rest-primes [2]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment