-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-prime.clj
60 lines (52 loc) · 1.4 KB
/
util-prime.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(load-file "../util.clj")
(defn is-prime?
[n]
(cond
(<= n 1)
false
(= n 2)
true
:else
(->> (range 2 (inc (Math/sqrt n)))
(filter (partial mod-zero? n))
(count)
(zero?))))
(assert (is-prime? 7))
(assert (is-prime? 11))
(assert (not (is-prime? 4)))
(assert (not (is-prime? -14)))
(assert (is-prime? 2))
(assert (not (is-prime? 1)))
(defn partially-truncatable-prime?
[n f]
(if (= (count (str n)) 1)
false
(loop [s (str n)]
(cond
(empty? s)
true
(not (is-prime? (read-string s)))
false
:else
(recur (apply str (f s)))))))
(assert (partially-truncatable-prime? 3797 rest))
(assert (partially-truncatable-prime? 23 rest))
(assert (not (partially-truncatable-prime? 7 rest)))
(assert (not (partially-truncatable-prime? 24 rest)))
(assert (partially-truncatable-prime? 3797 drop-last))
(assert (partially-truncatable-prime? 23 drop-last))
(assert (not (partially-truncatable-prime? 7 drop-last)))
(assert (not (partially-truncatable-prime? 24 drop-last)))
(defn is-truncatable-prime?
[n]
(and (partially-truncatable-prime? n rest) (partially-truncatable-prime? n drop-last)))
(assert (is-truncatable-prime? 3797))
(assert (is-truncatable-prime? 23))
(def valid-digits [1 2 3 5 7 9])
(defn cartesian-product
[f a b]
(map
(fn
[a0]
(map (partial f a0) b))
a))