I'm trying to write a function in Clojure that finds the first x prime numbers. I wrote these functions:
(defn isprime? [n]
(if (empty? (filter #(= 0 (mod n %)) (range 2 n)))
n
0)
)
(defn listprimes [nums]
(if (first nums)
(cons (isprime? (first nums)) (listprimes (rest nums)))
[])
)
The first one checks if a given number is prime or not, retruns it if it is or 0 id it isn't. The second gets a vector of numbers and activates the first function on each element. So, if my input to listprimes
is [1 2 3 4 5 6] the output will be [1 2 3 0 5 0].
I was planning to use filter
in the following way (for any x):
(take x (filter #(== 0 %) (listprimes(iterate inc 0)))
But I get a StackOverflow.. Any ideas what am I doing wrong?
Thanks!