-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_60.clj
53 lines (40 loc) · 1.33 KB
/
problem_60.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
(ns clj-test.core
(:use [clojure.math.combinatorics])
(:use [clojure.set]))
(def certainty 5)
(defn prime? [n]
(.isProbablePrime (BigInteger/valueOf n) certainty))
(defmacro concat-int [a b]
`(read-string (str ~a ~b)))
(defn gen-primes [to]
(filter prime? (range to)))
(defn connected? [p1 p2]
(and (prime? (concat-int p1 p2)) (prime? (concat-int p2 p1))))
(defn primes-connected-with [primes p]
(set (filter (partial connected? p) primes)))
(defn connected-primes-map [primes]
(into {} (map #(vector % (primes-connected-with primes %)) primes)))
(def primes-map
(connected-primes-map (gen-primes 10000)))
(def primes (set (keys primes-map)))
(defn filter-neighbours [graph X v]
(intersection X (get graph v)))
(defn bron-kerbosch [graph clique-cb R P X]
(if (and (empty? P) (empty? X))
(clique-cb R)
(loop [ P P
X X ]
(if-let [v (first P)]
(do
(bron-kerbosch graph
clique-cb
(conj R v)
(filter-neighbours graph P v)
(filter-neighbours graph X v))
(recur (disj P v)
(conj X v)))))))
(defn print-result [X]
(if (= (count X) 5)
(println "Answer:" (reduce + 0 X))))
(defn -main []
(bron-kerbosch primes-map print-result #{} primes #{}))