-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_72.clj
45 lines (37 loc) · 921 Bytes
/
problem_72.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns clj-test.core)
(use 'clojure.contrib.math)
(defn gen-primes-map [n]
(let [reduce-one-prime
(fn [primes-map p]
(if (contains? primes-map p)
primes-map
(reduce #(assoc %1 %2 false)
(assoc primes-map p true)
(range (* p 2) (inc n) p))))]
(reduce reduce-one-prime
(reduce-one-prime {} 2)
(range 3 (inc n) 2))))
(def N (expt 10 6))
(def primes-map (gen-primes-map N))
(def mgcd (memoize gcd))
(defn phi
([n]
(if (primes-map n)
(dec n)
(phi n 2)))
([n d]
(if (<= n d)
(dec d)
(if (= (mod n d) 0)
(let [m (/ n d)
g (mgcd m d)]
(/ (* (phi m) (phi d) g) (phi g)))
(recur n (inc d))))))
(defn fractions-count [n]
(->> (inc n)
(range 2)
(map phi)
(reduce +)))
(println "Answer:" (fractions-count N))
(defn -main []
)