One more try.  This time using the int-map contrib library. (Thanks to Zack 
Tellman.) 

Warning: I'm assuming that the int-set seq is automagically sorted so I can 
reliably turn it back into a vector without sorting.  It seems to be stable at 
least.  I wanted a vector for fast nth access.  (Nth and count are constant 
time on vectors.)  I thought it would be slow to make the vector every round, 
but it runs pretty fast.  For small values of max, it was necessary to guard 
the nth access with the not-found nil.

(require '[clojure.data.int-map :as im])

(defn lucky3
  ([max] (lucky3 1 (im/dense-int-set (range 1 max 2))))
  ([i iset]
   (let [v (vec (seq iset))
         n (nth v i nil)]
     (if (and n (<= n (count v)))
       (recur (inc i) (reduce (fn [sss m] (disj sss (nth v m)))
                              iset
                              (range (dec n) (count v) n)))
       (sequence iset)))))


user=> (time (count (lucky3 1e6)))
"Elapsed time: 48905.491951 msecs"
71918

-- 
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.

Reply via email to