57
|
1 module root2 where
|
|
2
|
141
|
3 open import Data.Nat
|
|
4 open import Data.Nat.Properties
|
57
|
5 open import Data.Empty
|
141
|
6 open import Data.Unit using (⊤ ; tt)
|
57
|
7 open import Relation.Nullary
|
|
8 open import Relation.Binary.PropositionalEquality
|
141
|
9 open import Relation.Binary.Definitions
|
57
|
10
|
|
11 even : (n : ℕ ) → Set
|
141
|
12 even zero = ⊤
|
|
13 even (suc zero) = ⊥
|
|
14 even (suc (suc n)) = even n
|
57
|
15
|
|
16 even? : (n : ℕ ) → Dec ( even n )
|
141
|
17 even? zero = yes tt
|
|
18 even? (suc zero) = no (λ ())
|
|
19 even? (suc (suc n)) = even? n
|
|
20
|
|
21 n+even : {n m : ℕ } → even n → even m → even ( n + m )
|
|
22 n+even {zero} {zero} tt tt = tt
|
|
23 n+even {zero} {suc m} tt em = em
|
|
24 n+even {suc (suc n)} {m} en em = n+even {n} {m} en em
|
|
25
|
|
26 n*even : {m n : ℕ } → even n → even ( m * n )
|
|
27 n*even {zero} {n} en = tt
|
|
28 n*even {suc m} {n} en = n+even {n} {m * n} en (n*even {m} {n} en)
|
|
29
|
|
30 even*n : {n m : ℕ } → even n → even ( n * m )
|
|
31 even*n {n} {m} en = subst even (*-comm m n) (n*even {m} {n} en)
|
|
32
|
|
33 gcd1 : ( i i0 j j0 : ℕ ) → ℕ
|
|
34 gcd1 zero i0 zero j0 = i0
|
|
35 gcd1 zero i0 (suc zero) j0 = 1
|
|
36 gcd1 zero zero (suc (suc j)) j0 = j0
|
|
37 gcd1 zero (suc i0) (suc (suc j)) j0 = gcd1 i0 (suc i0) (suc j) (suc (suc j))
|
|
38 gcd1 (suc zero) i0 zero j0 = 1
|
|
39 gcd1 (suc (suc i)) i0 zero zero = i0
|
|
40 gcd1 (suc (suc i)) i0 zero (suc j0) = gcd1 (suc i) (suc (suc i)) j0 (suc j0)
|
|
41 gcd1 (suc i) i0 (suc j) j0 = gcd1 i i0 j j0
|
|
42
|
|
43 gcd : ( i j : ℕ ) → ℕ
|
|
44 gcd i j = gcd1 i i j j
|
57
|
45
|
141
|
46 even→gcd=2 : {n : ℕ} → even n → n > 0 → gcd n 2 ≡ 2
|
|
47 even→gcd=2 {suc (suc zero)} en (s≤s z≤n) = refl
|
|
48 even→gcd=2 {suc (suc (suc (suc n)))} en (s≤s z≤n) = begin
|
|
49 gcd (suc (suc (suc (suc n)))) 2
|
|
50 ≡⟨⟩
|
|
51 gcd (suc (suc n)) 2
|
|
52 ≡⟨ even→gcd=2 {suc (suc n)} en (s≤s z≤n) ⟩
|
|
53 2
|
|
54 ∎ where open ≡-Reasoning
|
57
|
55
|
142
|
56 open import nat
|
141
|
57
|
143
|
58 gcd24' : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n m ≡ gcd (n - m) m
|
|
59 gcd24' = {!!}
|
|
60
|
142
|
61 gcd24 : { n m k : ℕ} → n > 1 → m > 1 → k > 1 → gcd n k ≡ k → gcd m k ≡ k → ¬ ( gcd n m ≡ 1 )
|
|
62 gcd24 {n} {m} {k} 1<n 1<m 1<k gn gm gnm = gcd21 n n m m k k 1<n 1<m 1<k gn gm gnm where
|
|
63 gcd22 : ( i i0 o o0 : ℕ ) → gcd1 (suc i) i0 (suc o) o0 ≡ gcd1 i i0 o o0
|
|
64 gcd22 zero i0 zero o0 = refl
|
|
65 gcd22 zero i0 (suc o) o0 = refl
|
|
66 gcd22 (suc i) i0 zero o0 = refl
|
|
67 gcd22 (suc i) i0 (suc o) o0 = refl
|
143
|
68 1<ss : {j : ℕ} → 1 < suc (suc j)
|
|
69 1<ss = s≤s (s≤s z≤n)
|
142
|
70 gcd21 : ( i i0 j j0 o o0 : ℕ ) → 1 < i0 → 1 < j0 → 1 < o0 → gcd1 i i0 o o0 ≡ k → gcd1 j j0 o o0 ≡ k → gcd1 i i0 j j0 ≡ 1 → ⊥
|
144
|
71 gcd21 zero i0 zero j0 o o0 1<i 1<j 1<o refl gm gnm = nat-≡< (sym gnm) 1<i
|
|
72 gcd21 zero i0 (suc j) j0 o o0 1<i 1<j 1<o refl gm gnm = {!!}
|
142
|
73 gcd21 (suc i) i0 zero j0 o o0 1<i 1<j 1<o gn refl gnm = {!!}
|
|
74 gcd21 (suc i) i0 (suc j) j0 zero o0 1<i 1<j 1<o gn gm gnm = {!!}
|
|
75 gcd21 (suc i) i0 (suc j) j0 (suc o) o0 1<i 1<j 1<o gn gm gnm =
|
|
76 gcd21 i i0 j j0 o o0 1<i 1<j 1<o (subst (λ m → m ≡ k) (gcd22 i i0 _ _ ) gn)
|
|
77 (subst (λ m → m ≡ k) (gcd22 j j0 _ _ ) gm) (subst (λ k → k ≡ 1) (gcd22 i i0 _ _ ) gnm)
|
141
|
78
|
|
79 record Even (i : ℕ) : Set where
|
|
80 field
|
|
81 j : ℕ
|
|
82 is-twice : i ≡ 2 * j
|
|
83
|
|
84 e2 : (i : ℕ) → even i → Even i
|
|
85 e2 zero en = record { j = 0 ; is-twice = refl }
|
|
86 e2 (suc (suc i)) en = record { j = suc (Even.j (e2 i en )) ; is-twice = e21 } where
|
|
87 e21 : suc (suc i) ≡ 2 * suc (Even.j (e2 i en))
|
|
88 e21 = begin
|
|
89 suc (suc i) ≡⟨ cong (λ k → suc (suc k)) (Even.is-twice (e2 i en)) ⟩
|
|
90 suc (suc (2 * Even.j (e2 i en))) ≡⟨ sym (*-distribˡ-+ 2 1 _) ⟩
|
|
91 2 * suc (Even.j (e2 i en)) ∎ where open ≡-Reasoning
|
|
92
|
|
93 record Odd (i : ℕ) : Set where
|
|
94 field
|
|
95 j : ℕ
|
|
96 is-twice : i ≡ suc (2 * j )
|
57
|
97
|
141
|
98 odd2 : (i : ℕ) → ¬ even i → even (suc i)
|
|
99 odd2 zero ne = ⊥-elim ( ne tt )
|
|
100 odd2 (suc zero) ne = tt
|
|
101 odd2 (suc (suc i)) ne = odd2 i ne
|
|
102
|
|
103 odd3 : (i : ℕ) → ¬ even i → Odd i
|
|
104 odd3 zero ne = ⊥-elim ( ne tt )
|
|
105 odd3 (suc zero) ne = record { j = 0 ; is-twice = refl }
|
|
106 odd3 (suc (suc i)) ne = record { j = Even.j (e2 (suc i) (odd2 i ne)) ; is-twice = odd31 } where
|
|
107 odd31 : suc (suc i) ≡ suc (2 * Even.j (e2 (suc i) (odd2 i ne)))
|
|
108 odd31 = begin
|
|
109 suc (suc i) ≡⟨ cong suc (Even.is-twice (e2 (suc i) (odd2 i ne))) ⟩
|
|
110 suc (2 * (Even.j (e2 (suc i) (odd2 i ne)))) ∎ where open ≡-Reasoning
|
|
111
|
|
112 odd4 : (i : ℕ) → even i → ¬ even ( suc i )
|
|
113 odd4 (suc (suc i)) en en1 = odd4 i en en1
|
|
114
|
|
115 even^2 : {n : ℕ} → even ( n * n ) → even n
|
|
116 even^2 {n} en with even? n
|
|
117 ... | yes y = y
|
|
118 ... | no ne = ⊥-elim ( odd4 ((2 * m) + 2 * m * suc (2 * m)) (n+even {2 * m} {2 * m * suc (2 * m)} ee3 ee4) (subst (λ k → even k) ee2 en )) where
|
|
119 m : ℕ
|
|
120 m = Odd.j ( odd3 n ne )
|
|
121 ee3 : even (2 * m)
|
|
122 ee3 = subst (λ k → even k ) (*-comm m 2) (n*even {m} {2} tt )
|
|
123 ee4 : even ((2 * m) * suc (2 * m))
|
|
124 ee4 = even*n {(2 * m)} {suc (2 * m)} (even*n {2} {m} tt )
|
|
125 ee2 : n * n ≡ suc (2 * m) + ((2 * m) * (suc (2 * m) ))
|
|
126 ee2 = begin n * n ≡⟨ cong ( λ k → k * k) (Odd.is-twice (odd3 n ne)) ⟩
|
|
127 suc (2 * m) * suc (2 * m) ≡⟨ *-distribʳ-+ (suc (2 * m)) 1 ((2 * m) ) ⟩
|
|
128 (1 * suc (2 * m)) + 2 * m * suc (2 * m) ≡⟨ cong (λ k → k + 2 * m * suc (2 * m)) (begin
|
|
129 suc m + 1 * m + 0 * (suc m + 1 * m ) ≡⟨ +-comm (suc m + 1 * m) 0 ⟩
|
|
130 suc m + 1 * m ≡⟨⟩
|
|
131 suc (2 * m)
|
|
132 ∎) ⟩ suc (2 * m) + 2 * m * suc (2 * m) ∎ where open ≡-Reasoning
|
57
|
133
|
141
|
134 open import nat
|
|
135
|
|
136 e3 : {i j : ℕ } → 2 * i ≡ 2 * j → i ≡ j
|
|
137 e3 {zero} {zero} refl = refl
|
|
138 e3 {suc x} {suc y} eq with <-cmp x y
|
|
139 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≡< eq (s≤s (<-trans (<-plus a) (<-plus-0 (s≤s (<-plus a ))))))
|
|
140 ... | tri≈ ¬a b ¬c = cong suc b
|
|
141 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym eq) (s≤s (<-trans (<-plus c) (<-plus-0 (s≤s (<-plus c ))))))
|
|
142
|
|
143 record Rational : Set where
|
|
144 field
|
|
145 i j : ℕ
|
|
146 coprime : gcd i j ≡ 1
|
57
|
147
|
142
|
148 root2-irrational : ( n m : ℕ ) → n > 1 → m > 1 → 2 * n * n ≡ m * m → ¬ (gcd n m ≡ 1)
|
|
149 root2-irrational n m n>1 m>1 2nm = gcd24 {n} {m} n>1 m>1 (s≤s (s≤s z≤n)) (even→gcd=2 rot7 (rot5 n>1)) (even→gcd=2 ( even^2 {m} ( rot1)) (rot5 m>1))where
|
|
150 rot5 : {n : ℕ} → n > 1 → n > 0
|
|
151 rot5 {n} lt = <-trans a<sa lt
|
141
|
152 rot1 : even ( m * m )
|
|
153 rot1 = subst (λ k → even k ) rot4 (n*even {n * n} {2} tt ) where
|
|
154 rot4 : (n * n) * 2 ≡ m * m
|
|
155 rot4 = begin
|
|
156 (n * n) * 2 ≡⟨ *-comm (n * n) 2 ⟩
|
|
157 2 * ( n * n ) ≡⟨ sym (*-assoc 2 n n) ⟩
|
|
158 2 * n * n ≡⟨ 2nm ⟩
|
|
159 m * m ∎ where open ≡-Reasoning
|
|
160 E : Even m
|
|
161 E = e2 m ( even^2 {m} ( rot1 ))
|
|
162 rot2 : 2 * n * n ≡ 2 * Even.j E * m
|
|
163 rot2 = subst (λ k → 2 * n * n ≡ k * m ) (Even.is-twice E) 2nm
|
|
164 rot3 : n * n ≡ Even.j E * m
|
|
165 rot3 = e3 ( begin
|
|
166 2 * (n * n) ≡⟨ sym (*-assoc 2 n _) ⟩
|
|
167 2 * n * n ≡⟨ rot2 ⟩
|
|
168 2 * Even.j E * m ≡⟨ *-assoc 2 (Even.j E) m ⟩
|
|
169 2 * (Even.j E * m) ∎ ) where open ≡-Reasoning
|
|
170 rot7 : even n
|
|
171 rot7 = even^2 {n} (subst (λ k → even k) (sym rot3) ((n*even {Even.j E} {m} ( even^2 {m} ( rot1 )))))
|
57
|
172
|