Or there's this approach, based on an example from the docs
<https://clojuredocs.org/clojure.core/lazy-seq>, which gives an infinite
lazy seq of primes:
(defn divisible-by? [x y]
(zero? (mod x y)))
(defn sieve [s]
(cons (first s)
(lazy-seq
(sieve (filter #(not (divisible-by? % (first s)))
(rest s))))))
(def primes
(cons 2
(sieve (iterate (partial + 2) 3))))
Caveat: 'infinite' here means 'about 1500 items before the stack blows up',
but you get the idea.
As to actually implementing Eratosthenes, you might try translating this
take on it <http://forums.xkcd.com/viewtopic.php?f=11&t=104862#p3452685>
(Python) into a functional style (here's my humble attempt
<https://gist.github.com/ahammel/6680067> at a C++ translation). The idea
is to lazily maintain a set of the smallest known composites you're likely
to encounter, updating as you go along. You may also be interested in the
awe-inspiring paper The Genuine Sieve of Eratosthenes
<http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf> (PDF).
On Wed, Nov 26, 2014 at 3:02 PM, bernardH <[email protected]>
wrote:
> Hi,
> As has been said, this is not SoE. But my take would be something along :
> (reduce
> (fn [primes number]
> (if (some zero? (map (partial mod number) (take-while #(<= %
> (Math/sqrt number)) primes)))
> primes
> (conj primes number)))
> [2]
> (take n (iterate inc 3)))
>
> But I'm a noob, so it migth be the bilnd leading the blind ☺
>
> Best Regards,
>
> b.
>
>
> On Wednesday, November 26, 2014 3:52:44 PM UTC+1, Chernyshev Alex wrote:
>>
>> (defn not-divisible-by?[num denum]
>> (not (= (mod num denum) 0)))
>>
>> (defn div-nums [denum bound]
>> (for [x (range 2 bound) :when (not-divisible-by? x denum)] x))
>>
>> (defn divisible? [coll denum]
>> (empty? (filter #(and (not= denum %) (not(not-divisible-by? denum %)))
>> coll)))
>>
>> (defn generate-primes
>> "Sieve of Eratosthenes"
>> [n]
>> (let [src (div-nums 2 n)]
>> (cons 2 (for [x src :when (divisible? src x)] x))))
>>
>>
>> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.