Skip to content

Instantly share code, notes, and snippets.

@amalloy
Forked from darklajid/gist:1246123
Created September 27, 2011 20:33
Show Gist options
  • Save amalloy/1246161 to your computer and use it in GitHub Desktop.
Save amalloy/1246161 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"
[]
(letfn [(reinsert [table x prime]
(let [key (+ prime x)]
(update-in table key conj prime)))
(primes-step [table d]
(if-let [factors (get table d)]
(let [new-table (dissoc table d)]
(recur (reduce #(reinsert %1 d %2)
new-table
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