Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created June 4, 2009 19:22
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 jimweirich/123787 to your computer and use it in GitHub Desktop.
Save jimweirich/123787 to your computer and use it in GitHub Desktop.
(defn reject-multiples
"A function that rejects multiples of n."
[n]
(fn [x] (not (zero? (mod x n)))))
(defn primes
"An infinite sequence of prime numbers"
([] (primes (iterate inc 2)))
([seq]
(let [head (first seq)]
(lazy-seq
(cons head
(primes (filter (reject-multiples head) (rest seq))) )))))
; Print the first prime > 1000
(println (first (drop-while #(< % 1000) (primes))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment