DEV Community

Discussion on: Write a program or script to find Lucky Numbers

joshavg profile image
Josha von Gizycki

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
                     (fn [ix item]
                       (when (not= 0 (mod (inc ix) ln)) item))
        (recur new-ws (nth new-ws step) (inc step)))
      (println (count working-set)))))