Why not turn this problem on its head? Have a vector of lists of prime factors for each i. March through this vector and for each prime factor in the current list, add that prime factor p to list i + p. And if i has no prime factors, then it is a prime itself and add it to the list at 2 * i.
So the only test for prime is if i has no prime factors as determined by propagating the prime factors forward, not through a mod function. On Wednesday, September 23, 2015 at 8:14:39 PM UTC-4, Charles Reese wrote: > > I want to compute a lazy sequence of primes. > > Here is the interface: > > user=> (take 10 primes) > (2 3 5 7 11 13 17 19 23 29) > > So far, so good. > > However, when I take 500 primes, this results in a stack overflow. > > core.clj: 133 clojure.core/seq > core.clj: 2595 clojure.core/filter/fn > LazySeq.java: 40 clojure.lang.LazySeq/sval > LazySeq.java: 49 clojure.lang.LazySeq/seq > RT.java: 484 clojure.lang.RT/seq > core.clj: 133 clojure.core/seq > core.clj: 2626 clojure.core/take/fn > LazySeq.java: 40 clojure.lang.LazySeq/sval > LazySeq.java: 49 clojure.lang.LazySeq/seq > Cons.java: 39 clojure.lang.Cons/next > LazySeq.java: 81 clojure.lang.LazySeq/next > RT.java: 598 clojure.lang.RT/next > core.clj: 64 clojure.core/next > core.clj: 2856 clojure.core/dorun > core.clj: 2871 clojure.core/doall > core.clj: 2910 clojure.core/partition/fn > LazySeq.java: 40 clojure.lang.LazySeq/sval > LazySeq.java: 49 clojure.lang.LazySeq/seq > RT.java: 484 clojure.lang.RT/seq > core.clj: 133 clojure.core/seq > core.clj: 2551 clojure.core/map/fn > LazySeq.java: 40 clojure.lang.LazySeq/sval > LazySeq.java: 49 clojure.lang.LazySeq/seq > RT.java: 484 clojure.lang.RT/seq > core.clj: 133 clojure.core/seq > core.clj: 3973 clojure.core/interleave/fn > LazySeq.java: 40 clojure.lang.LazySeq/sval > > I'm wondering what is the problem here and, more generally, when working > with lazy sequences, how should I approach this class of error? > > Here is the code. > > (defn assoc-nth > "Returns a lazy seq of coll, replacing every nth element by val > > Ex: > user=> (assoc-nth [3 4 5 6 7 8 9 10] 2 nil) > (3 nil 5 nil 7 nil 9 nil) > " > [coll n val] > (apply concat > (interleave > (map #(take (dec n) %) (partition n coll)) (repeat [val])))) > > (defn sieve > "Returns a lazy seq of primes by Eratosthenes' method > > Ex: > user=> (take 4 (sieve (iterate inc 2))) > (2 3 5 7) > > user=> (take 10 (sieve (iterate inc 2))) > (2 3 5 7 11 13 17 19 23 29) > " > [s] > (lazy-seq > (if (seq s) > (cons (first s) (sieve > (drop-while nil? (assoc-nth (rest s) (first s) > nil)))) > []))) > > (def primes > "Returns a lazy seq of primes > > Ex: > user=> (take 10 primes) > (2 3 5 7 11 13 17 19 23 29) > " > (concat [2] (sieve (filter odd? (iterate inc 3))))) > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.