-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path050.clj
24 lines (20 loc) · 797 Bytes
/
050.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
(use '[clojure.contrib.lazy-seqs :only (primes)])
(defn prime? [n]
(if (> 2 n)
false
(not-any? #(zero? (rem n %)) (take-while #(<= (* % %) n) primes))))
(defn make-seq-accumulator [seq]
(map first (iterate (fn [[sum s]]
[(+ sum (first s)) (next s)])
[(first seq) (rest seq)])))
(def prime-sums (conj (make-seq-accumulator primes) 0))
(defn euler050 [target]
(loop [c 1]
(let [bots (reverse (take c prime-sums))
tops (take c (reverse (take-while #(> target (- % (last bots)))
(rest prime-sums))))]
(if-let [v (some #(if (prime? %) % )
(map #(- %1 %2) tops bots))]
v
(recur (inc c))))))
(time (println (euler050 1000000)))