Do you want the check if your initial code is working correctly? Because
when I ran it, this is the output with 100 numbers:
(1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79 87 93 99)
I have modified it like this. I have highlighted what I have changed.:
(defn indexed-sieve [index this-list]
(keep-indexed
(fn [i v]
(if (= 0 (mod (inc i) index))
nil
v))
this-list))
(defn lucky-numbers [max-value]
(let [current-list *(atom (range 1 max-value)) *
current-index *(atom 2)*]
(while (< @current-index (count @current-list))
(reset! current-list (indexed-sieve *@current-index* @current-list))
(reset! current-index (inc @current-index)))
@current-list))
This give the following output:
(1 3 7 13 19 27 39 49 63 79 91)
This seems more correct.
You can further convert the while to loop recur to avoid using atoms.
On Wed, Feb 18, 2015 at 5:28 AM, Cecil Westerhof <[email protected]>
wrote:
> 2015-02-18 0:28 GMT+01:00 Steve Miner <[email protected]>:
>
>>
>> > On Feb 17, 2015, at 4:39 PM, Colin Jones <[email protected]> wrote:
>> >
>> > Sounds almost like a mirror image of `clojure.core/take-nth`, so
>> something like this is kind of fun:
>> >
>> > (defn drop-nth [n coll]
>> > (lazy-seq
>> > (when-let [s (seq coll)]
>> > (concat (take (dec n) s) (drop-nth n (drop n s))))))
>> >
>>
>> I like that drop-nth. Here's my version of lucky:
>>
>> (defn lucky
>> "Returns sequence of Lucky numbers less than max"
>> ([max] (when (pos? max) (lucky 1 (range 1 max 2))))
>> ([i acc]
>> (if-let [n (nth acc i nil)]
>> (recur (inc i) (drop-nth n acc))
>> acc)))
>>
>> The recur in this case loops back to the top.
>>
>
> It looks good, but with 1000000 I get a stack overflow. And even with
> 100000. It takes also about two times as long to execute.
>
> --
> Cecil Westerhof
>
> --
> 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.