Skip to content

Instantly share code, notes, and snippets.

@velveteer
Last active November 15, 2015 21:33
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 velveteer/6e07a9de45be1d5f2a23 to your computer and use it in GitHub Desktop.
Save velveteer/6e07a9de45be1d5f2a23 to your computer and use it in GitHub Desktop.
(ns sumofprimes)
(def primes "Infinite, lazy sequence of prime numbers."
((fn step [m n]
(or
(some-> (get m n) (-> (->> (reduce #(update %1 (+ %2 n) conj %2) (dissoc m n))) (step (inc n))))
(-> (assoc m (* n n) (list n)) (step (inc n)) (->> (cons n) (lazy-seq)))
)) {} 2))
; without threading
(def optimus "Infinite, lazy sequence of prime numbers."
((fn step [m n]
(if-let [factors (get m n)]
(recur (reduce #(update %1 (+ %2 n) conj %2) (dissoc m n) factors) (inc n))
(lazy-seq (cons n (step (assoc m (* n n) (list n)) (inc n))))
)) {} 2))
(println (reduce + (take 1000 primes)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment