Tuesday, September 2, 2014

Euler Project: Problem 7 in Clojure

This is my solution in Clojure to the seventh problem from Euler project:

(defn prime? [n]
(loop [n n prime 2 factors []]
(if (> (count factors) 1)
false
(cond
(= n 1) true
(factor? n prime) (recur (/ n prime) prime (conj factors prime))
:else (recur n (inc prime) factors)))))
(def nums (iterate inc 2))
(last (take 10001 (filter prime? nums))) ; 104743
view raw euler-7.clj hosted with ❤ by GitHub

It works but it's quite slow.

No comments:

Post a Comment