Skip to content

Instantly share code, notes, and snippets.

@pepijndevos
Created January 25, 2011 18:42
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 pepijndevos/795374 to your computer and use it in GitHub Desktop.
Save pepijndevos/795374 to your computer and use it in GitHub Desktop.
(ns test)
(def primes (letfn [(composite? [primes n]
(some #(zero? (rem n %)) primes))
(next-prime [primes]
(first (drop-while
(partial composite? primes)
(range (peek primes) Double/POSITIVE_INFINITY))))]
(map peek (iterate #(conj % (next-prime %)) [2]))))
(defn primes2
([] (primes2 (range 3 Double/POSITIVE_INFINITY 2)))
([s]
(let [n (first s)]
(lazy-seq
(cons n (primes2 (remove
#(some (partial = %)
(take-while (partial > %)
(iterate (partial + n) n)))
(rest s))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment