Created
October 26, 2010 00:05
-
-
Save gfredericks/646063 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
(defn- add-seq-at | |
[colls new-seq] | |
(let [target (first new-seq) | |
existing (or (colls target) [])] | |
(assoc colls target (cons new-seq existing)))) | |
(defn- check-stuff | |
[seq-seq-map z] | |
(let [ssmz (seq-seq-map z)] | |
(if ssmz | |
(let [d (dissoc seq-seq-map z) | |
new-ssmz | |
(reduce | |
(fn [seq-seq [_ & cs]] | |
(add-seq-at seq-seq cs)) | |
d | |
ssmz)] | |
[new-ssmz true]) | |
[seq-seq-map false]))) | |
(defn next-prime | |
[{:keys [multiple-seqs ints primes]}] | |
(let [p (first primes) | |
p-squared (* p p) | |
[z & zs] ints | |
[new-seqs is-comp?] (check-stuff multiple-seqs z) | |
[new-new-seqs new-primes] | |
(if (= (first zs) p-squared) | |
[(add-seq-at new-seqs (iterate #(+ (* 2 p) %) p-squared)) (rest primes)] | |
[new-seqs primes]) | |
next-ob | |
{:multiple-seqs new-new-seqs, | |
:ints zs, | |
:primes new-primes, | |
:p z}] | |
(if is-comp? | |
(recur next-ob) | |
next-ob))) | |
(defn all-primes | |
[] | |
(cons 2 | |
(map :p | |
(iterate next-prime | |
{:multiple-seqs {}, | |
:ints (iterate #(+ % 2) 5), | |
:primes (drop 1 (lazy-seq (all-primes))), | |
:p 3})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment