2015-02-19 23:25 GMT+01:00 Steve Miner <stevemi...@gmail.com>: > I found a better data structure for calculating lucky numbers. The > contrib library data.avl (from Michał Marczyk) has a persistent sorted-set > that supports nth. This runs much faster than my previous attempts. > > (require '[clojure.data.avl :as avl]) > > (defn lucky-avl > ([max] (lucky-avl 1 (apply avl/sorted-set (range 1 max 2)))) > ([i avl] > (let [n (nth avl i nil)] > (if (and n (<= n (count avl))) > (recur (inc i) (reduce (fn [sss m] (disj sss (nth avl m))) > avl > (range (dec n) (count avl) n))) > (sequence avl))))) > > (time (count (lucky-avl 1e6))) > "Elapsed time: 2396.158435 msecs" > 71918 >
It works especially well for bigger values. With 1e7 I got with the old version: "Elapsed time: 3924244.877112 msecs" and with this version: "Elapsed time: 14863.904489 msecs" More as 250 times as fast. -- 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.