/
algo.clj
70 lines (49 loc) · 1.5 KB
/
algo.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
61
62
63
64
65
66
67
68
69
70
(ns gamma.algo
(:require [clojure.test :refer :all]))
(def prime-to-100
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97])
(defn is-prime? [n]
(let [possible-factors (range 2 n)
remainders (map #(mod n %) possible-factors)]
(not (some zero? remainders))))
(is-prime? 5)
;; -> true
(is-prime? 6)
;; -> false
(defn gen-primes [n]
(filter is-prime? (range 2 (inc n))))
(deftest test-prime-numbers
(is (= prime-to-100 (gen-primes 100))))
(defn prime-reaction [[a b]]
(if (and (> a b)
(zero? (mod a b)))
[(/ a b) b]
[a b]))
(prime-reaction [6 2])
;; -> [3 2]
(prime-reaction [5 2])
;; -> [5 2]
(def molecules (range 2 101))
(defn mix-and-react [mols]
(let [mixed (partition 2 (shuffle mols))
reacted (map prime-reaction mixed)]
(flatten reacted)))
(take 10 (mix-and-react molecules))
;; -> (37 48 87 46 38 91 68 13 39 33)
(defn reaction-cycle [n]
(loop [i n
mols molecules]
(if (zero? i)
mols
(recur (dec i) (mix-and-react mols)))))
(take 10 (reaction-cycle 100))
;; -> (2 2 11 23 2 2 5 3 79 17)
(sort (distinct (reaction-cycle 1000)))
;; -> (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)
(deftest test-prime-reaction-cycle
(let [reactions (reaction-cycle 10000)]
(is (= prime-to-100 (-> reactions distinct sort)))))
(let [reactions (reaction-cycle 10000)]
(-> reactions distinct sort))
;; -> (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)
(run-tests)