re: Write a program or script to find Lucky Numbers VIEW POST

TOP OF THREAD FULL DISCUSSION
re: As always: happy to provide a Clojure solution: (defn luckies [n] (loop [working-set (range 1 (inc n)) ln 2 step 1] (if ...
 

Got it down to still very slow 100 seconds by using the vec function on the sieved result to make it a vector instead of a linked list. This way the nth call inside the recur statement is way faster.

(defn luckies [n]
  (loop [working-set (range 1 (inc n))
         ln 2
         step 1]
    (if (< ln (count working-set))
      (let [new-ws (vec
                    (keep-indexed
                     (fn [ix item]
                       (when (not= 0 (mod (inc ix) ln)) item))
                     working-set))]
        (recur new-ws (nth new-ws step) (inc step)))
      (println (count working-set)))))
code of conduct - report abuse