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
|
148
|
11 open import gcd
|
159
|
12 open import even
|
142
|
13 open import nat
|
141
|
14
|
|
15 record Rational : Set where
|
|
16 field
|
|
17 i j : ℕ
|
|
18 coprime : gcd i j ≡ 1
|
57
|
19
|
187
|
20 even→gcd=2 : {n : ℕ} → even n → gcd n 2 ≡ 2
|
|
21 even→gcd=2 {zero} en = refl
|
|
22 even→gcd=2 {suc (suc zero)} en = refl
|
|
23 even→gcd=2 {suc (suc (suc (suc n)))} en = begin
|
159
|
24 gcd (suc (suc (suc (suc n)))) 2 ≡⟨⟩
|
187
|
25 gcd (suc (suc n)) 2 ≡⟨ even→gcd=2 {suc (suc n)} en ⟩
|
159
|
26 2 ∎ where open ≡-Reasoning
|
|
27
|
|
28 even^2 : {n : ℕ} → even ( n * n ) → even n
|
|
29 even^2 {n} en with even? n
|
|
30 ... | yes y = y
|
|
31 ... | 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
|
|
32 m : ℕ
|
|
33 m = Odd.j ( odd3 n ne )
|
|
34 ee3 : even (2 * m)
|
|
35 ee3 = subst (λ k → even k ) (*-comm m 2) (n*even {m} {2} tt )
|
|
36 ee4 : even ((2 * m) * suc (2 * m))
|
|
37 ee4 = even*n {(2 * m)} {suc (2 * m)} (even*n {2} {m} tt )
|
|
38 ee2 : n * n ≡ suc (2 * m) + ((2 * m) * (suc (2 * m) ))
|
|
39 ee2 = begin n * n ≡⟨ cong ( λ k → k * k) (Odd.is-twice (odd3 n ne)) ⟩
|
|
40 suc (2 * m) * suc (2 * m) ≡⟨ *-distribʳ-+ (suc (2 * m)) 1 ((2 * m) ) ⟩
|
|
41 (1 * suc (2 * m)) + 2 * m * suc (2 * m) ≡⟨ cong (λ k → k + 2 * m * suc (2 * m)) (begin
|
|
42 suc m + 1 * m + 0 * (suc m + 1 * m ) ≡⟨ +-comm (suc m + 1 * m) 0 ⟩
|
|
43 suc m + 1 * m ≡⟨⟩
|
|
44 suc (2 * m)
|
|
45 ∎) ⟩ suc (2 * m) + 2 * m * suc (2 * m) ∎ where open ≡-Reasoning
|
|
46
|
|
47 e3 : {i j : ℕ } → 2 * i ≡ 2 * j → i ≡ j
|
|
48 e3 {zero} {zero} refl = refl
|
|
49 e3 {suc x} {suc y} eq with <-cmp x y
|
|
50 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≡< eq (s≤s (<-trans (<-plus a) (<-plus-0 (s≤s (<-plus a ))))))
|
|
51 ... | tri≈ ¬a b ¬c = cong suc b
|
|
52 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym eq) (s≤s (<-trans (<-plus c) (<-plus-0 (s≤s (<-plus c ))))))
|
|
53
|
165
|
54 open Factor
|
164
|
55
|
186
|
56 -- gcd-div : ( i j k : ℕ ) → (if : Factor k i) (jf : Factor k j )
|
|
57 -- → remain if ≡ 0 → remain jf ≡ 0
|
|
58 -- → Dividable k ( gcd i j )
|
|
59
|
142
|
60 root2-irrational : ( n m : ℕ ) → n > 1 → m > 1 → 2 * n * n ≡ m * m → ¬ (gcd n m ≡ 1)
|
198
|
61 root2-irrational n m n>1 m>1 2nm = rot13 ( gcd-div n m 2 (s≤s (s≤s z≤n)) {!!} {!!} ) where
|
165
|
62 rot13 : {m : ℕ } → Dividable 2 m → m ≡ 1 → ⊥
|
185
|
63 rot13 d refl with Dividable.factor d | Dividable.is-factor d
|
|
64 ... | zero | ()
|
|
65 ... | suc n | ()
|
165
|
66 rot11 : {m : ℕ } → even m → Factor 2 m
|
|
67 rot11 {zero} em = record { factor = 0 ; remain = 0 ; is-factor = refl }
|
|
68 rot11 {suc zero} ()
|
|
69 rot11 {suc (suc m) } em = record { factor = suc (factor fc ) ; remain = remain fc ; is-factor = isfc } where
|
|
70 fc : Factor 2 m
|
|
71 fc = rot11 {m} em
|
|
72 isfc : suc (factor fc) * 2 + remain fc ≡ suc (suc m)
|
|
73 isfc = begin
|
|
74 suc (factor fc) * 2 + remain fc ≡⟨ cong (λ k → k + remain fc) (*-distribʳ-+ 2 1 (factor fc)) ⟩
|
|
75 ((1 * 2) + (factor fc)* 2 ) + remain fc ≡⟨⟩
|
|
76 ((1 + 1) + (factor fc)* 2 ) + remain fc ≡⟨ cong (λ k → k + remain fc) (+-assoc 1 1 _ ) ⟩
|
|
77 (1 + (1 + (factor fc)* 2 )) + remain fc ≡⟨⟩
|
|
78 suc (suc ((factor fc * 2) + remain fc )) ≡⟨ cong (λ x → suc (suc x)) (is-factor fc) ⟩
|
|
79 suc (suc m) ∎ where open ≡-Reasoning
|
142
|
80 rot5 : {n : ℕ} → n > 1 → n > 0
|
|
81 rot5 {n} lt = <-trans a<sa lt
|
141
|
82 rot1 : even ( m * m )
|
|
83 rot1 = subst (λ k → even k ) rot4 (n*even {n * n} {2} tt ) where
|
|
84 rot4 : (n * n) * 2 ≡ m * m
|
|
85 rot4 = begin
|
|
86 (n * n) * 2 ≡⟨ *-comm (n * n) 2 ⟩
|
|
87 2 * ( n * n ) ≡⟨ sym (*-assoc 2 n n) ⟩
|
|
88 2 * n * n ≡⟨ 2nm ⟩
|
|
89 m * m ∎ where open ≡-Reasoning
|
|
90 E : Even m
|
|
91 E = e2 m ( even^2 {m} ( rot1 ))
|
|
92 rot2 : 2 * n * n ≡ 2 * Even.j E * m
|
|
93 rot2 = subst (λ k → 2 * n * n ≡ k * m ) (Even.is-twice E) 2nm
|
|
94 rot3 : n * n ≡ Even.j E * m
|
|
95 rot3 = e3 ( begin
|
|
96 2 * (n * n) ≡⟨ sym (*-assoc 2 n _) ⟩
|
|
97 2 * n * n ≡⟨ rot2 ⟩
|
|
98 2 * Even.j E * m ≡⟨ *-assoc 2 (Even.j E) m ⟩
|
|
99 2 * (Even.j E * m) ∎ ) where open ≡-Reasoning
|
|
100 rot7 : even n
|
|
101 rot7 = even^2 {n} (subst (λ k → even k) (sym rot3) ((n*even {Even.j E} {m} ( even^2 {m} ( rot1 )))))
|
165
|
102 f2 : Factor 2 n
|
|
103 f2 = rot11 rot7
|
185
|
104 f3 : ( n : ℕ) → (e : even n ) → remain (rot11 {n} e) ≡ 0
|
|
105 f3 zero e = refl
|
|
106 f3 (suc (suc n)) e = f3 n e
|
165
|
107 fm : Factor 2 m
|
185
|
108 fm = record { factor = Even.j E ; remain = 0 ; is-factor = fm1 } where
|
|
109 fm1 : Even.j E * 2 + 0 ≡ m
|
|
110 fm1 = begin
|
|
111 Even.j E * 2 + 0 ≡⟨ +-comm _ 0 ⟩
|
|
112 Even.j E * 2 ≡⟨ *-comm (Even.j E) 2 ⟩
|
|
113 2 * Even.j E ≡⟨ sym ( Even.is-twice E ) ⟩
|
|
114 m ∎ where open ≡-Reasoning
|
57
|
115
|
185
|
116
|