Skip to content
Snippets Groups Projects
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
Branches
No related tags found
No related merge requests found
......@@ -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)))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment