Skip to content

Instantly share code, notes, and snippets.

@Netpilgrim
Created August 28, 2011 01:40
Show Gist options
  • Save Netpilgrim/1176126 to your computer and use it in GitHub Desktop.
Save Netpilgrim/1176126 to your computer and use it in GitHub Desktop.
Lazy seq of primes
(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