Skip to content

Instantly share code, notes, and snippets.

@emdeesee
Last active December 7, 2016 20:20
Show Gist options
  • Save emdeesee/4333778 to your computer and use it in GitHub Desktop.
Save emdeesee/4333778 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
(defn gen-primes []
(letfn [(reinsert [table x prime]
(update-in table [(+ prime x)] conj prime))
(step [table d]
(if-let [factors (table d)]
(recur (reduce #(reinsert %1 d %2) (dissoc table d) factors)
(inc d))
(lazy-seq (cons d
(step (assoc table (* d d) (list d)) (inc d))))))]
(step {} 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment