Commit a41dbbba authored by nimrod's avatar nimrod
Browse files

The Clojure version is now more functional, no mutable state (and without any stack overflows).

parent f6c2f9b0
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -10,12 +10,17 @@
       (fn [y] (not= (mod x y) 0))
       (range 2 (math/ceil (math/sqrt x))))))

(defn primes-iter [n, i, v]
  (loop [cnt i acc v]
    (if (= (count acc) n) acc
      (recur (inc cnt) (if (prime? cnt) (conj acc cnt) acc)))))

(defn primes [n]
  (if
    (zero? n) []
    (primes-iter n 0 [])))


(defn -main
  [& args]
  (def v [])
  (def i 0)
  (while (< (count v) (Integer/parseInt (first args)))
    (cond (prime? i) (def v (conj v i)))
    (def i (inc i)))
  (println v))
  (println (primes (Integer/parseInt (first args)))))