2015-02-18 18:25 GMT+01:00 Cecil Westerhof <cldwester...@gmail.com>: > 2015-02-18 16:31 GMT+01:00 Tassilo Horn <t...@gnu.org>: > >> 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--- >> > > By the way: it goes wrong for values below 3. >
This way it works OK: (defn lucky-numbers "Lucky numbers from 1 up-to upto-value. 1 <= upto-value <= 10.000.000 http://en.wikipedia.org/wiki/Lucky_number" ; doc-string and pre-condition should match [upto] {:pre [(>= upto 1) (<= upto (* 10 1000 1000))]} (if (< upto 3) (list 1) (loop [coll (range 1 (inc upto) 2), survivor-idx 1] (let [i (nth coll survivor-idx)] (if (> (count coll) i) (recur (drop-every-nth coll i) (inc survivor-idx)) coll))))) I discovered pre-conditions. Makes the code a lot clearer. (I still work with a maximum value.) -- 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.