Last active
November 15, 2015 21:33
-
-
Save velveteer/6e07a9de45be1d5f2a23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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