-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path037.clj
30 lines (22 loc) · 896 Bytes
/
037.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(use '[clojure.contrib.lazy-seqs :only (primes)])
(defn prime? [n]
(if (> 2 n)
false
(not-any? #(zero? (rem n %)) (take-while #(<= (* % %) n) primes))))
(defn as-digits [num]
(map #(Character/getNumericValue %) (str num)))
(defn as-int [coll]
(Integer/parseInt (apply str coll)))
(defn lr-truncatable-prime? [n]
(let [trunc-prime? (fn [f n]
(cond
(< n 10) (prime? n)
;; recur with the same function and the
;; result of applying the function
(prime? n) (recur f (as-int (f (as-digits n))))
true false))]
(and (trunc-prime? butlast n) ; from right
(trunc-prime? rest n)))) ; from left
(defn euler037 []
(reduce + (take 11 (filter lr-truncatable-prime? (iterate inc 10)))))
(time (println (euler037)))