2015-02-18 16:31 GMT+01:00 Tassilo Horn <t...@gnu.org>: > Cecil Westerhof <cldwester...@gmail.com> writes: > > > One problem is that max-value is used in the doc-string also, so if it > > changes, it has to be done at two places. Is there a way around this? > > I wouldn't specify a max-value at all. Some users might have enough > time to wait for the result, have faster machines, might use the > function in some years in the future where we all have faster > machines... >
I will think about it. I like to take care of border situations (one of them waiting very long), but you have a point. > Here's my version: > > --8<---------------cut here---------------start------------->8--- > (defn drop-every-nth [c n] > (keep-indexed (fn [i el] > (if (zero? (mod (inc i) n)) nil el)) > c)) > > (defn lucky-numbers [n] > (loop [c (range 1 (inc n) 2), survivor-idx 1] > (let [i (nth c survivor-idx)] > (if (> (count c) i) > (recur (drop-every-nth c i) (inc survivor-idx)) > c)))) > --8<---------------cut here---------------end--------------->8--- > > Runtimes of that version: > > --8<---------------cut here---------------start------------->8--- > user> (lucky-numbers 100) > (1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79 87 93 99) > user> (time (dorun (lucky-numbers 100))) > "Elapsed time: 0.079083 msecs" > nil > user> (time (dorun (lucky-numbers 1000))) > "Elapsed time: 0.690399 msecs" > nil > user> (time (dorun (lucky-numbers 100000))) > "Elapsed time: 1023.293714 msecs" > nil > user> (time (dorun (lucky-numbers 1000000))) > "Elapsed time: 63413.245417 msecs" > nil > --8<---------------cut here---------------end--------------->8--- > > That version is about a factor of 10 times faster than your version on > my system (which I renamed to lucky-numbers-cecil): > I see the same performance increase. > Since our two versions are almost identical from an algorithmical point > of view, the slowdown of your version seems to be caused entirely by the > overhead of atoms. > I did not like to work with atoms, but I did not know another way. I will look carefully at your code and learn something. :-) -- Cecil Westerhof -- 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.