-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquare_Root_Prime
265 lines (218 loc) · 5.98 KB
/
Square_Root_Prime
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import tactic
import data.nat.prime
import data.int.basic
open classical
-- HUMAN PROOF 1:
--Claim: Let n > 1. If n has no positive PRIME factor less ≤ sqrt(n), then n is prime
-- Proof by Contradiction:
-- Suppose n is not prime. Then n has atleast two positive prime factors p1 and p2 such that
-- n = p1p2*k for some positive integer k. By hypothesis p1 and p2 are > sqrt(n). Then
-- n = p1p2*k ≥ p1p2 > n, and so n > n. Contradiction
-- HUMAN PROOF 2:
-- Claim: Let n > 1. If n has no positive PROPER factor less ≤ sqrt(n), then n is prime
-- Proof my Contrapositive:
-- Suppose that n is composite. Then n = a*b where a ≤ b and b < n. Hence a^2 ≤ ab = n.
-- Hence a ≤ sqrt(n).
--Important theorems adapted from the Natural Number Game------------------------------
theorem ge_iff_exists_add (a b: ℕ): a ≥ b ↔ ∃ (c: ℕ), a = b + c :=
begin
apply le_iff_exists_add, -- Ok, maybe this theorem was a little bit silly :)
end
def greater (a b: ℕ): Prop := a ≥ b ∧ a ≠ b
axiom zero (a : ℕ): a < 1 ↔ a =0
axiom zero2 (a u : ℕ): u + a = u ↔ a = 0
theorem g_iff_exists_add (a b: ℕ): greater a b ↔ ∃ (c: ℕ), a = b + c ∧ c ≥ 1:=
begin --Perhaps a little bit overkill
rw greater,
rw ge_iff_exists_add,
rw ne_from_not_eq,
split,
{ intro h,
have h_left: ∃ (c : ℕ), a = b + c, from and.left h,
have h_right: ¬a = b, from and.right h,
cases h_left with u hu,
use u,
split,
apply hu,
by_contradiction,
push_neg at a_1,
rw zero at a_1,
subst a_1,
rw add_zero at hu,
have h_right: a ≠ b, from and.right h,
exact h_right hu,
},
{ intro h2,
cases h2 with u hu,
have hu_left: a = b+u, from and.left hu,
split,
use u,
apply hu_left,
by_contradiction,
subst hu_left,
rw zero2 at a_1,
rw ← zero at a_1,
have hu_right: u ≥ 1, from and.right hu,
linarith, -- Thanks Olu :)
}
end
theorem my_simp (u v a : ℕ): u*v + (a*a + u*a) = a*u + (u*v + a*a):=
begin
rw ← add_assoc,
rw ← add_assoc,
rw add_comm,
simp,
rw mul_comm,
end
-----------------------------------------------------------------------------------------
def dvd (m n: ℕ): Prop := ∃ k, n = m * k
def prime (p : ℕ) : Prop := p ≥ 2 ∧ ∀ n, dvd n p → n = 1 ∨ n = p
def composite (d : ℕ): Prop:= ∃ (a b: ℕ), d = a*b ∧ a ≤ b ∧ b < d
theorem ge_refl(x : ℕ) : x ≥ x :=
begin
rw ge_iff_exists_add,
use 0,
rw add_zero,
end
theorem ge_cancel (a b : ℕ) (ha : a*b ≥ a) (hb: a ≥ 1) : b ≥ 1 :=
begin
rw ge_iff_exists_add at ha,
rw ge_iff_exists_add at hb,
cases ha with u hu,
cases hb with v hv,
rw ge_iff_exists_add,
----more work needs to be done... but thankfully I dont need it :)-------
end
theorem example1 (a b c :ℕ) (hba: b ≥ a) (hca: c ≥ a) : b*c ≥ a*a :=
begin
rw ge_iff_exists_add at hba,
rw ge_iff_exists_add at hca,
cases hba with u hu,
cases hca with v hv,
rw ge_iff_exists_add,
use a*v + a*u + u*v,
rw add_comm,
rw hv,
rw hu,
rw mul_add,
rw add_mul,
rw add_mul,
rw add_comm,
simp,
rw my_simp,
end
-- Adapted from the Natural Number Game---
lemma one_eq_succ_zero: 1 = nat.succ(0) :=
begin
refl, -- OK maybe a little bit silly :)
end
lemma succ_eq_add_one (n : ℕ) : nat.succ n = n + 1 :=
begin
induction n with d hd,
rw zero_add,
rw hd,
--A little bit surprising Lean did this quickly. In the game it took me these lines:
--induction n with d hd,
--rw one_eq_succ_zero,
--rw zero_add,
--refl,
--rw hd,
--rw ← succ_add,
--rw hd,
--refl,
end
----------------------------------------------------------------------
lemma x_plus_stuff_plus_one_ge_x (x d : ℕ) : x + d + 1 ≥ x :=
-- Helps to prove my_lemma_helper
begin
rw ge_iff_exists_add,
use d+1,
rw add_assoc,
end
lemma my_lemma_helper (x y: ℕ) : y+x ≥ x :=
begin
induction y with d hd,
rw zero_add,
apply ge_refl,
rw add_comm,
rw nat.add_succ,
rw add_comm at hd,
rw succ_eq_add_one,
apply x_plus_stuff_plus_one_ge_x,
end
lemma my_lemma (c x : ℕ) (hab: c >= 1): c * x >= x :=
begin
rw ge_iff_exists_add at hab,
cases hab with d hd,
rw hd,
rw add_mul,
rw one_mul,
rw add_comm,
rw mul_comm,
apply my_lemma_helper,
end
theorem example2 (a b c : ℕ) (hba: c ≥ 1): a*b*c ≥ a*b :=
begin
rw mul_comm,
apply my_lemma,
exact hba,
end
theorem example3 (a:ℕ) : ¬ a < a :=
begin
push_neg,
apply le_iff_lt_or_eq.2,
right,
refl,
-- This should help with the contradiction
end
theorem square_root_prime1 (n p : ℕ) (hn : n > 1) (hp : prime p ∧ dvd p n → p*p > n) : ¬ composite n :=
begin
--HUMAN PROOF 1 in Code :(
intro h,
rw composite at h,
cases h with a ha,
cases ha with b hb,
have hbleft: n = a*b, from and.left hb,
have hbmidright: a ≤ b ∧ prime a, from and.right hb,
have hbright: prime a, from and.right hbmidright,
have hbmid: a ≤ b, from and.left hbmidright,
have hadn : dvd a n,
rw dvd,
use b,
rw hbleft,
--:= \<_, hbleft\>,
---by_contradiction h,
end
lemma another_helper (u v : ℕ) : v ≤ u ↔ v*v ≤ v*u :=
begin
cases v,
{ split,
{ intro h,
simp,
},
intro, exact zero_le u,
},
{
symmetry,
apply mul_le_mul_left,
simp,
}
end
theorem square_root_prime2 (n : ℕ) (h1: composite n): ∃ (c d : ℕ), n = c*d ∧ c*c ≤ n :=
-- HUMAN PROOF 2 in Code :)
begin
rw composite at h1,
cases h1 with v hv,
cases hv with u hu,
use v,
use u,
have hl: n = v*u, from and.left hu,
rw hl,
have hmr: v ≤ u ∧ u < n, from and.right hu,
have hr: u < n, from and.right hmr,
have hm: v ≤ u, from and.left hmr,
rw another_helper at hm,
split,
refl,
apply hm,
end