mathematics_in_lean

My solutions for this book

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. 254
import data.nat.prime
import algebra.big_operators
import tactic

open_locale big_operators

theorem two_le {m : } (h0 : m  0) (h1 : m  1) : 2  m :=
begin
  cases m, contradiction,
  cases m, contradiction,
  repeat { apply nat.succ_le_succ },
  apply zero_le
end

example {m : } (h0 : m  0) (h1 : m  1) : 2  m :=
begin
  by_contradiction h,
  push_neg at h,
  interval_cases m; contradiction
end

example {m : } (h0 : m  0) (h1 : m  1) : 2  m :=
begin
  by_contradiction h,
  push_neg at h,
  revert m h h0 h1,
  dec_trivial
end

example {m : } (h : m < 2) : m = 0  m = 1 :=
by dec_trivial!

example {m : } (h0 : m  0) (h1 : m  1) : 2  m :=
by omega

theorem exists_prime_factor {n : nat} (h : 2  n) :
   p : nat, p.prime  p  n :=
begin
  by_cases np : n.prime,
  { use [n, np, dvd_rfl] },
  induction n using nat.strong_induction_on with n ih,
  dsimp at ih,
  rw nat.prime_def_lt at np,
  push_neg at np,
  rcases np h with m, mltn, mdvdn, mne1,
  have : m  0,
  { intro mz,
    rw [mz, zero_dvd_iff] at mdvdn,
    linarith },
  have mgt2 : 2  m := two_le this mne1,
  by_cases mp : m.prime,
  { use [m, mp, mdvdn] },
  rcases ih m mltn mgt2 mp with p, pp, pdvd,
  use [p, pp, pdvd.trans mdvdn]
end

theorem primes_infinite :  n,  p > n, nat.prime p :=
begin
  intro n,
  have : 2  nat.factorial (n + 1) + 1,
    sorry,
  rcases exists_prime_factor this with p, pp, pdvd,
  refine p, _, pp,
  show p > n,
  by_contradiction ple, push_neg at ple,
  have : p  nat.factorial (n + 1),
    sorry,
  have : p  1,
    sorry,
  show false,
    sorry
end

open finset

section
variables {α : Type*} [decidable_eq α] (r s t : finset α)

example : r  (s  t)  (r  s)  (r  t) :=
begin
  rw subset_iff,
  intro x,
  rw [mem_inter, mem_union, mem_union, mem_inter, mem_inter],
  tauto
end

example : r  (s  t)  (r  s)  (r  t) :=
by { simp [subset_iff], intro x, tauto }

example : (r  s)  (r  t)  r  (s  t) :=
by { simp [subset_iff], intro x, tauto }

example : (r  s)  (r  t) = r  (s  t) :=
by { ext x, simp, tauto }

end

section
variables {α : Type*} [decidable_eq α] (r s t : finset α)

example : (r  s)  (r  t) = r  (s  t) :=
sorry

example : (r \ s \ t) = r \ (s  t) :=
sorry

end
example (s : finset ) (n : ) (h : n  s) : n  ( i in s, i) :=
finset.dvd_prod_of_mem _ h

theorem nat.prime.eq_of_dvd_of_prime {p q : }
    (prime_p : nat.prime p) (prime_q : nat.prime q) (h : p  q) :
  p = q :=
sorry

theorem mem_of_dvd_prod_primes {s : finset } {p : } (prime_p : p.prime) :
  ( n  s, nat.prime n)   (p   n in s, n)  p  s :=
begin
  intros h₀ h₁,
  induction s using finset.induction_on with a s ans ih,
  { simp at h₁,
    linarith [prime_p.two_le] },
  simp [finset.prod_insert ans, prime_p.dvd_mul] at h₀ h₁,
  rw mem_insert,
  sorry
end

example (s : finset ) (x : ) : x  s.filter nat.prime  x  s  x.prime :=
mem_filter

theorem primes_infinite' :  (s : finset nat),  p, nat.prime p  p  s :=
begin
  intro s,
  by_contradiction h,
  push_neg at h,
  set s' := s.filter nat.prime with s'_def,
  have mem_s' :  {n : }, n  s'  n.prime,
  { intro n,
    simp [s'_def],
    apply h },
  have : 2  ( i in s', i) + 1,
    sorry,
  rcases exists_prime_factor this with p, pp, pdvd,
  have : p  ( i in s', i),
    sorry,
  have : p  1,
  { convert nat.dvd_sub' pdvd this, simp },
  show false,
    sorry
end

theorem bounded_of_ex_finset (Q :   Prop):
  ( s : finset ,  k, Q k  k  s)   n,  k, Q k  k < n :=
begin
  rintros s, hs,
  use s.sup id + 1,
  intros k Qk,
  apply nat.lt_succ_of_le,
  show id k  s.sup id,
  apply le_sup (hs k Qk)
end

theorem ex_finset_of_bounded (Q :   Prop) [decidable_pred Q] :
  ( n,  k, Q k  k  n)  ( s : finset ,  k, Q k  k  s) :=
begin
  rintros n, hn,
  use (range (n + 1)).filter Q,
  intro k,
  simp [nat.lt_succ_iff],
  exact hn k
end

example : 27 % 4 = 3 := by norm_num

example (n : ) : (4 * n + 3) % 4 = 3 :=
by { rw [add_comm, nat.add_mul_mod_self_left], norm_num }

theorem mod_4_eq_3_or_mod_4_eq_3 {m n : } (h : m * n % 4 = 3) :
  m % 4 = 3  n % 4 = 3 :=
begin
  revert h,
  rw [nat.mul_mod],
  have : m % 4 < 4 := nat.mod_lt m (by norm_num),
  interval_cases m % 4 with hm; simp [hm],
  have : n % 4 < 4 := nat.mod_lt n (by norm_num),
  interval_cases n % 4 with hn; simp [hn]; norm_num
end

theorem two_le_of_mod_4_eq_3 {n : } (h : n % 4 = 3) : 2  n :=
by apply two_le; { intro neq, rw neq at h, norm_num at h }

theorem aux {m n : } (h₀ : m  n) (h₁ : 2  m) (h₂ : m < n) :
  (n / m)  n  n / m < n :=
sorry

theorem exists_prime_factor_mod_4_eq_3 {n : nat} (h : n % 4 = 3) :
   p : nat, p.prime  p  n  p % 4 = 3 :=
begin
  by_cases np : n.prime,
  { use [n, np, dvd_rfl, h] },
  induction n using nat.strong_induction_on with n ih,
  dsimp at ih,
  rw nat.prime_def_lt at np,
  push_neg at np,
  rcases np (two_le_of_mod_4_eq_3 h) with m, mltn, mdvdn, mne1,
  have mge2 : 2  m,
  { apply two_le _ mne1,
    intro mz,
    rw [mz, zero_dvd_iff] at mdvdn, linarith },
  have neq : m * (n / m) = n := nat.mul_div_cancel' mdvdn,
  have : m % 4 = 3  (n / m) % 4 = 3,
  { apply mod_4_eq_3_or_mod_4_eq_3, rw [neq, h] },
  cases this with h1 h1,
  { sorry },
  sorry
end

example (m n : ) (s : finset ) (h : m  erase s n) : m  n  m  s :=
by rwa mem_erase at h

example (m n : ) (s : finset ) (h : m  erase s n) : m  n  m  s :=
by { simp at h, assumption }

theorem primes_mod_4_eq_3_infinite :  n,  p > n, nat.prime p  p % 4 = 3 :=
begin
  by_contradiction h,
  push_neg at h,
  cases h with n hn,
  have :  s : finset nat,  p : , p.prime  p % 4 = 3  p  s,
  { apply ex_finset_of_bounded,
    use n,
    contrapose! hn,
    rcases hn with p, pp, p4, pltn,
    exact p, pltn, pp, p4 },
  cases this with s hs,
  have h₀ : 2  4 * ( i in erase s 3, i) + 3,
    sorry,
  have h₁ : (4 * ( i in erase s 3, i) + 3) % 4 = 3,
    sorry,
  rcases exists_prime_factor_mod_4_eq_3 h₁ with p, pp, pdvd, p4eq,
  have ps : p  s,
    sorry,
  have pne3 : p  3,
    sorry,
  have : p  4 * ( i in erase s 3, i),
    sorry,
  have : p  3,
    sorry,
  have : p = 3,
    sorry,
  contradiction
end