Skip to content

Instantly share code, notes, and snippets.

@darklajid
Created September 27, 2011 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darklajid/1246123 to your computer and use it in GitHub Desktop.
Save darklajid/1246123 to your computer and use it in GitHub Desktop.
Prime number generator in clojure, port from F#
(defn primes []
"Generates an (infinite, lazy) sequence of primes"
(let [reinsert (fn [table x prime]
(let [key (+ prime x)]
(assoc table key (conj (get table key) prime))))]
(letfn [(primes-step [table d]
(if-let [factors (get table d)]
(let [newTable (dissoc table d)]
(recur (reduce #(reinsert %1 d %2) newTable factors) (inc d)))
(lazy-seq (cons d (primes-step (assoc table (* d d) (list d)) (inc d))))))]
(primes-step {} 2))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment