-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path049.clj
31 lines (25 loc) · 975 Bytes
/
049.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
31
(use '[clojure.contrib.lazy-seqs :only (primes)])
(use 'clojure.contrib.combinatorics)
(use '[clojure.contrib.math :only (abs)])
(defn prime? [n]
(if (> 2 n)
false
(not-any? #(zero? (rem n %)) (take-while #(<= (* % %) n) primes))))
(defn as-int [coll] (Integer/parseInt (apply str coll)))
(defn primes-permuted [prime]
(sort (distinct (filter #(and (prime? %) (<= 1000 %))
(map as-int (permutations (str prime)))))))
(defn progression? [coll start step]
(and (some #(= (+ start step) %) coll)
(some #(= (+ start step step) %) coll)))
(defn euler049 []
(let [prime-permutations
(distinct (map primes-permuted
(take-while #(< % 10000)
(drop-while #(< 1000 %) primes))))]
(for [a prime-permutations
b a
c (range 1000 (- 10000 b b))
:when (progression? a b c)]
[b (+ b c) (+ b c c)])))
(time (println (euler049)))