Lean / Reader
Lean reader

Theodorus.lean

584 lines51 declarationsno sorryimports Mathlib.Analysis.SpecialFunctions.Trigonometric.ArctanDeriv Mathlib.Analysis.Real.Pi.Bounds Mathlib.Analysis.Calculus.Deriv.MeanValue Mathlib.Tactic
  1. import Mathlib.Analysis.SpecialFunctions.Trigonometric.ArctanDeriv
  2. import Mathlib.Analysis.Real.Pi.Bounds
  3. import Mathlib.Analysis.Calculus.Deriv.MeanValue
  4. import Mathlib.Tactic
  5. /-! Analytic estimates for the discrete Theodorus spiral, OEIS A172164. -/
  6. namespace Theodorus
  7. open Real
  8. noncomputable def angle (m : ℕ) : ℝ :=
  9. ∑ j ∈ Finset.range m, Real.arctan (1 / Real.sqrt ((j : ℝ) + 1))
  10. def Crossing (n m : ℕ) : Prop :=
  11. angle (m - 1) ≤ 2 * Real.pi * n ∧ 2 * Real.pi * n < angle m
  12. noncomputable def winding (n : ℕ) : ℕ :=
  13. if n = 0 then 0 else sInf {m : ℕ | 2 * Real.pi * n < angle m}
  14. noncomputable def windingDifference (n : ℕ) : ℤ :=
  15. (winding (n+2) : ℤ) - 2 * winding (n+1) + winding n
  16. /-- Dumortier's published four-value conjecture; index zero here corresponds to OEIS index two. -/
  17. def FourWindingDifferences : Prop :=
  18. Set.range windingDifference = ({18, 19, 20, 21} : Set ℤ)
  19. theorem arctan_bounds {x : ℝ} (hx : 0 ≤ x) :
  20. x - x^3/3 ≤ Real.arctan x ∧ Real.arctan x ≤ x - x^3/3 + x^5/5 := by
  21. have hL : Monotone (fun t : ℝ => Real.arctan t - (t - t^3/3)) := by
  22. apply monotone_of_hasDerivAt_nonneg (f' := fun t => 1/(1+t^2) - (1-3*t^2/3))
  23. · intro t
  24. convert (Real.hasDerivAt_arctan t).sub
  25. ((hasDerivAt_id t).sub (((hasDerivAt_id t).pow 3).div_const 3)) using 1; simp
  26. · intro t
  27. change 0 ≤ _
  28. have hd : (0 : ℝ) < 1+t^2 := by positivity
  29. field_simp [hd.ne']
  30. nlinarith [sq_nonneg (t^2)]
  31. have hU : Monotone (fun t : ℝ => t - t^3/3 + t^5/5 - Real.arctan t) := by
  32. apply monotone_of_hasDerivAt_nonneg (f' := fun t => (1-3*t^2/3+5*t^4/5) - 1/(1+t^2))
  33. · intro t
  34. convert (((hasDerivAt_id t).sub (((hasDerivAt_id t).pow 3).div_const 3)).add
  35. (((hasDerivAt_id t).pow 5).div_const 5)).sub (Real.hasDerivAt_arctan t) using 1; simp
  36. · intro t
  37. change 0 ≤ _
  38. have hd : (0 : ℝ) < 1+t^2 := by positivity
  39. field_simp [hd.ne']
  40. nlinarith [sq_nonneg (t^3)]
  41. have L := hL hx
  42. have U := hU hx
  43. norm_num at L U
  44. constructor <;> linarith
  45. /-- Quadratic approximation of a square root with its explicit one-sided remainder. -/
  46. theorem sqrt_remainder {z : ℝ} (hz : 0 ≤ z) :
  47. 0 ≤ 1 + z/2 - Real.sqrt (1+z) ∧
  48. 1 + z/2 - Real.sqrt (1+z) ≤ z^2/8 := by
  49. let w := Real.sqrt (1+z)
  50. have hw : w^2 = 1+z := Real.sq_sqrt (by linarith)
  51. have h1 : 1 ≤ w := by
  52. have hp : 0 ≤ w := Real.sqrt_nonneg _
  53. nlinarith
  54. have h2 : w ≤ 1+z/2 := by nlinarith [sq_nonneg (w-1)]
  55. have hi : (1+z/2-w)*(1+z/2+w) = z^2/4 := by nlinarith
  56. change 0 ≤ 1+z/2-w ∧ 1+z/2-w ≤ z^2/8
  57. constructor
  58. · linarith
  59. · nlinarith [mul_nonneg (sub_nonneg.mpr h2) (show 0 ≤ (1+z/2+w)-2 by linarith)]
  60. theorem normalized_sqrt_increment {t : ℝ} (ht : 0 ≤ t) :
  61. let S := Real.sqrt (1+7*t/6) + Real.sqrt (1+t/6)
  62. 1-t/3 ≤ 2/S ∧ 2/S ≤ 1-t/3 + t^2/5 := by
  63. let S := Real.sqrt (1+7*t/6) + Real.sqrt (1+t/6)
  64. change 1-t/3 ≤ 2/S ∧ 2/S ≤ 1-t/3 + t^2/5
  65. have ha := sqrt_remainder (z := 7*t/6) (by positivity)
  66. have hb := sqrt_remainder (z := t/6) (by positivity)
  67. have h₁ : 1 ≤ Real.sqrt (1+7*t/6) := by
  68. rw [Real.le_sqrt (by norm_num) (by positivity)]; linarith
  69. have h₂ : 1 ≤ Real.sqrt (1+t/6) := by
  70. rw [Real.le_sqrt (by norm_num) (by positivity)]; linarith
  71. have hS : 2 ≤ S := by dsimp [S]; linarith
  72. have hSp : 0 < S := by linarith
  73. have hSu : S ≤ 2+2*t/3 := by dsimp [S]; linarith [ha.1,hb.1]
  74. have hSl : 2+2*t/3-S ≤ 25*t^2/144 := by dsimp [S]; nlinarith [ha.2,hb.2]
  75. constructor
  76. · apply (le_div_iff₀ hSp).2
  77. nlinarith [mul_nonneg ht (sub_nonneg.mpr hS)]
  78. · apply (div_le_iff₀ hSp).2
  79. nlinarith [mul_nonneg ht (sub_nonneg.mpr hSu),
  80. mul_nonneg (sq_nonneg t) (sub_nonneg.mpr hS),sq_nonneg t]
  81. /-- Compare a single angle with a successive difference of 2 sqrt(m+7/6). -/
  82. theorem angle_increment {m : ℝ} (hm : 0 < m) :
  83. |Real.arctan (1/Real.sqrt m) -
  84. (2*Real.sqrt (m+7/6) - 2*Real.sqrt (m+1/6))| ≤
  85. (1/Real.sqrt m)^5/5 := by
  86. let u := 1/Real.sqrt m
  87. have hr : 0 < Real.sqrt m := Real.sqrt_pos.2 hm
  88. have hu : 0 < u := one_div_pos.2 hr
  89. let t := u^2
  90. let A := Real.sqrt (1+7*t/6)
  91. let B := Real.sqrt (1+t/6)
  92. have hM : (Real.sqrt m)^2=m := Real.sq_sqrt hm.le
  93. have ht : t*m=1 := by dsimp [t,u]; field_simp [hr.ne']; exact hM.symm
  94. have hA : A^2=1+7*t/6 := Real.sq_sqrt (by dsimp [t]; positivity)
  95. have hB : B^2=1+t/6 := Real.sq_sqrt (by dsimp [t]; positivity)
  96. have hAp : 0 ≤ A := Real.sqrt_nonneg _
  97. have hBp : 0 ≤ B := Real.sqrt_nonneg _
  98. have Aeq : Real.sqrt (m+7/6) = Real.sqrt m * A := by
  99. apply (sq_eq_sq₀ (Real.sqrt_nonneg _) (mul_nonneg hr.le hAp)).1
  100. rw [Real.sq_sqrt (by positivity), mul_pow, hM, hA]; nlinarith
  101. have Beq : Real.sqrt (m+1/6) = Real.sqrt m * B := by
  102. apply (sq_eq_sq₀ (Real.sqrt_nonneg _) (mul_nonneg hr.le hBp)).1
  103. rw [Real.sq_sqrt (by positivity), mul_pow, hM, hB]; nlinarith
  104. have hpos : 0 < A + B := by dsimp [A,B,t]; positivity
  105. have huR : Real.sqrt m*u=1 := by dsimp [u]; field_simp
  106. have hdiff : 2*Real.sqrt (m+7/6)-2*Real.sqrt (m+1/6)= u*(2/(A+B)) := by
  107. rw [Aeq,Beq,← mul_div_assoc]
  108. apply (eq_div_iff hpos.ne').2
  109. calc
  110. _ = 2*Real.sqrt m*(A^2-B^2) := by ring
  111. _ = 2*Real.sqrt m * u^2 := by rw [hA,hB]; dsimp [t]; ring
  112. _ = u*2 := by nlinarith [congrArg (· * u) huR]
  113. have hnorm := normalized_sqrt_increment (t := t) (sq_nonneg u)
  114. change 1-t/3 ≤ 2/(A+B) ∧ 2/(A+B) ≤ 1-t/3 + t^2/5 at hnorm
  115. have hl := mul_le_mul_of_nonneg_left hnorm.1 hu.le
  116. have hh := mul_le_mul_of_nonneg_left hnorm.2 hu.le
  117. dsimp [t] at hl hh
  118. have han := arctan_bounds hu.le
  119. rw [hdiff]
  120. change |Real.arctan u - u*(2/(A+B))| ≤ u^5/5
  121. rw [abs_le]
  122. constructor <;> nlinarith [han.1,han.2]
  123. noncomputable def residual (m : ℕ) : ℝ := angle m - 2 * Real.sqrt ((m:ℝ)+7/6)
  124. noncomputable def budget (m : ℕ) : ℝ := (1/Real.sqrt (m:ℝ))^3/5
  125. theorem budget_step {m : ℕ} (hm : 0 < m) :
  126. (1/Real.sqrt ((m:ℝ)+1))^5/5 ≤ budget m - budget (m+1) := by
  127. let a := Real.sqrt (m:ℝ)
  128. let b := Real.sqrt ((m:ℝ)+1)
  129. have ha : 0 < a := Real.sqrt_pos.2 (by exact_mod_cast hm)
  130. have hb : 0 < b := Real.sqrt_pos.2 (by positivity)
  131. have hab : a ≤ b := Real.sqrt_le_sqrt (by linarith)
  132. have hA : a^2=(m:ℝ) := Real.sq_sqrt (by positivity)
  133. have hB : b^2=(m:ℝ)+1 := Real.sq_sqrt (by positivity)
  134. have hDiff : b^5 - a^3*b^2 = b^3*(b^2-a^2)+b^2*a^2*(b-a) := by ring
  135. have h1 : b^2-a^2=1 := by linarith
  136. rw [h1,mul_one] at hDiff
  137. have h2 : a^3 ≤ b^3 := by gcongr
  138. have h3 : 0 ≤ b^2*a^2*(b-a) := mul_nonneg (by positivity) (sub_nonneg.mpr hab)
  139. unfold budget
  140. push_cast
  141. change (1/b)^5/5 ≤ (1/a)^3/5 - (1/b)^3/5
  142. field_simp [ha.ne', hb.ne']
  143. nlinarith
  144. theorem residual_step {m : ℕ} (hm : 0 < m) :
  145. |residual (m+1) - residual m| ≤ budget m - budget (m+1) := by
  146. have h := angle_increment (m := (m:ℝ)+1) (by positivity)
  147. have h₂ := budget_step hm
  148. apply le_trans _ h₂
  149. unfold residual angle
  150. rw [Finset.sum_range_succ]
  151. push_cast
  152. convert h using 1; ring_nf
  153. theorem residual_gap {m M : ℕ} (hm : 0 < m) (hM : m ≤ M) :
  154. |residual M-residual m| ≤ budget m - budget M := by
  155. induction M, hM using Nat.le_induction with
  156. | base => simp
  157. | succ M hM ih =>
  158. have hs := residual_step (m := M) (by omega)
  159. calc |residual (M+1) - residual m|
  160. = |(residual (M+1)-residual M)+(residual M-residual m)| := by ring_nf
  161. _ ≤ |residual (M+1)-residual M|+|residual M-residual m| := abs_add_le _ _
  162. _ ≤ _ := by linarith
  163. theorem angle_monotone : Monotone angle := by
  164. apply monotone_nat_of_le_succ
  165. intro n
  166. simp only [angle,Finset.sum_range_succ]
  167. exact le_add_of_nonneg_right (Real.arctan_nonneg.2 (by positivity))
  168. theorem local_angle {m M : ℕ} (hm : 0 < m) (hM : m ≤ M) :
  169. |(angle m - residual M)-2*Real.sqrt ((m:ℝ)+7/6)| ≤ budget m := by
  170. have hb : 0 ≤ budget M := by unfold budget; positivity
  171. have h := residual_gap hm hM
  172. rw [abs_sub_comm] at h
  173. calc |(angle m - residual M)-2*Real.sqrt ((m:ℝ)+7/6)|
  174. = |residual m - residual M| := by unfold residual; ring_nf
  175. _ ≤ _ := by linarith
  176. /-- Squaring the angle estimate gives an error below 1/20 once m is at least 8. -/
  177. theorem squared_angle {m M : ℕ} (hm : 8 ≤ m) (hM : m ≤ M) :
  178. 0 < angle m - residual M ∧
  179. |((angle m - residual M)/2)^2-7/6-m| < 1/20 := by
  180. let a := Real.sqrt (m:ℝ)
  181. let b := Real.sqrt ((m:ℝ)+7/6)
  182. let d := (angle m - residual M)-2*b
  183. let e := budget m
  184. have h8 : (8:ℝ) ≤ m := by exact_mod_cast hm
  185. have ha : 0 < a := Real.sqrt_pos.2 (by linarith)
  186. have hA : a^2=(m:ℝ) := Real.sq_sqrt (by positivity)
  187. have hB : b^2=(m:ℝ)+7/6 := Real.sq_sqrt (by positivity)
  188. have hb : 0 ≤ b := Real.sqrt_nonneg _
  189. have ha1 : 1 ≤ a := by nlinarith
  190. have hb1 : 1 ≤ b := by nlinarith
  191. have hab : b ≤ 3*a/2 := by nlinarith
  192. have he : e=1/(5*a^3) := by dsimp [e,budget,a]; ring
  193. have hep : 0 < e := by rw [he]; positivity
  194. have he₁ : e ≤ 1/5 := by
  195. rw [he]
  196. apply one_div_le_one_div_of_le (by norm_num)
  197. nlinarith [show 1^3 ≤ a^3 by gcongr]
  198. have hea : e*a=1/(5*(m:ℝ)) := by
  199. rw [he,← hA]
  200. field_simp [ha.ne']
  201. have hem : 1/(5*(m:ℝ)) ≤ 1/40 := by
  202. apply one_div_le_one_div_of_le (by norm_num); linarith
  203. have heb : e*b ≤ 3/80 := by
  204. nlinarith [mul_le_mul_of_nonneg_left hab hep.le]
  205. have hD : |d| ≤ e := local_angle (by omega) hM
  206. have hd := abs_le.1 hD
  207. have hbd : -3/80 ≤ d*b ∧ d*b ≤ 3/80 := by
  208. constructor <;> nlinarith [mul_le_mul_of_nonneg_right hd.1 hb,
  209. mul_le_mul_of_nonneg_right hd.2 hb]
  210. have hd₁ : 0 ≤ 1/5-d := by linarith
  211. have hd₂ : 0 ≤ 1/5+d := by linarith
  212. have hdd : d^2 ≤ 1/25 := by nlinarith [mul_nonneg hd₁ hd₂]
  213. have hrel : angle m-residual M = 2*b+d := by dsimp [d]; ring
  214. rw [hrel]
  215. constructor
  216. · linarith
  217. · rw [abs_lt]
  218. constructor <;> nlinarith only [hB,hbd.1,hbd.2,hdd,sq_nonneg d]
  219. theorem exists_above (x : ℝ) : ∃ m : ℕ, x < angle m := by
  220. obtain ⟨N,hN⟩ := exists_nat_gt ((x+|residual 1|+2)^2)
  221. refine ⟨N+1, ?_⟩
  222. have h := residual_gap (m := 1) (M := N+1) (by decide) (by omega)
  223. have hb : budget 1=1/5 := by norm_num [budget]
  224. have hp : 0 ≤ budget (N+1) := by unfold budget; positivity
  225. have hdiff := (abs_le.1 h).1
  226. rw [hb] at hdiff
  227. unfold residual at hdiff
  228. have hs : 0 ≤ Real.sqrt ((N:ℝ)+1+7/6) := Real.sqrt_nonneg _
  229. have hsq := Real.sq_sqrt (x := (N:ℝ)+1+7/6) (by positivity)
  230. have hbnd : x+|residual 1|+2 < Real.sqrt ((N:ℝ)+1+7/6) := by nlinarith
  231. push_cast at hdiff
  232. have ha := neg_abs_le (residual 1)
  233. unfold residual at ha hbnd
  234. linarith
  235. theorem winding_spec {n : ℕ} (hn : 0 < n) : Crossing n (winding n) := by
  236. let S := {m : ℕ | 2 * Real.pi * n < angle m}
  237. have hS : S.Nonempty := exists_above _
  238. have hM : winding n=sInf S := by simp [winding,hn.ne',S]
  239. have hcross : 2*Real.pi*n < angle (winding n) := by
  240. rw [hM]; exact Nat.sInf_mem hS
  241. have hpos : 0 < winding n := by
  242. by_contra! h
  243. rw [Nat.le_zero.1 h] at hcross
  244. simp [angle] at hcross
  245. have : (0 : ℝ) ≤ 2*Real.pi*n := by positivity
  246. linarith
  247. refine ⟨?_,hcross⟩
  248. by_contra! h
  249. have hle : sInf S ≤ winding n-1 := Nat.sInf_le (show winding n-1 ∈ S from h)
  250. omega
  251. theorem winding_unique {m n : ℕ} (hn : 0 < n) (h : Crossing n m) : winding n = m := by
  252. have hw := winding_spec hn
  253. rcases lt_trichotomy (winding n) m with hlt|heq|hgt
  254. · have hi := angle_monotone (show winding n ≤ m-1 by omega)
  255. linarith [h.1,hw.2]
  256. · exact heq
  257. · have hi := angle_monotone (show m ≤ winding n-1 by omega)
  258. linarith [hw.1,h.2]
  259. theorem winding_monotone : Monotone winding := by
  260. intro n k hnk
  261. by_cases h : n=0
  262. · simp [h,winding]
  263. · have hn : 0 < n := by omega
  264. have hk : 0 < k := by omega
  265. have hN := winding_spec hn
  266. have hK := winding_spec hk
  267. by_contra! hbad
  268. have ha := angle_monotone (show winding k ≤ winding n - 1 by omega)
  269. have hangle : 2*Real.pi*n ≤ 2*Real.pi*k := by gcongr
  270. linarith [hN.1,hK.2]
  271. theorem angle_le (m : ℕ) : angle m ≤ m*(Real.pi/4) := by
  272. induction m with
  273. | zero => simp [angle]
  274. | succ m ih =>
  275. have h : 1 ≤ Real.sqrt ((m:ℝ)+1) := by
  276. rw [Real.le_sqrt (by norm_num) (by positivity)]; linarith
  277. have h' : 1/Real.sqrt ((m:ℝ)+1) ≤ 1 := by
  278. apply (div_le_iff₀ (by positivity)).2
  279. linarith
  280. have ha := Real.arctan_le_arctan h'
  281. rw [Real.arctan_one] at ha
  282. unfold angle
  283. rw [Finset.sum_range_succ]
  284. unfold angle at ih
  285. push_cast
  286. linarith
  287. theorem winding_lower {n : ℕ} (hn : 0 < n) : 9 ≤ winding n := by
  288. have hp := Real.pi_pos
  289. have hc := (winding_spec hn).2
  290. have ha := angle_le (winding n)
  291. by_contra! h
  292. have hw : (winding n:ℝ) ≤ 8 := by exact_mod_cast (show winding n ≤ 8 by omega)
  293. have hn' : (1:ℝ) ≤ n := by exact_mod_cast hn
  294. nlinarith [mul_nonneg (sub_nonneg.mpr hw) hp.le, mul_nonneg (sub_nonneg.mpr hn') hp.le]
  295. noncomputable def quad (K : ℝ) (n : ℕ) : ℝ := ((2*Real.pi*n-K)/2)^2-7/6
  296. theorem rounding {n M : ℕ} (hn : 0 < n) (hM : winding n ≤ M) :
  297. -(1/20) < winding n - quad (residual M) n ∧
  298. winding n - quad (residual M) n < 1+1/20 := by
  299. have hw := winding_lower hn
  300. have hc := winding_spec hn
  301. have hu := squared_angle (m := winding n) (by omega) hM
  302. have hl := squared_angle (m := winding n-1) (M := M) (by omega) (by omega)
  303. have hcast : ((winding n-1:ℕ):ℝ) = (winding n:ℝ)-1 := by
  304. rw [Nat.cast_sub (by omega)]; norm_num
  305. rw [hcast] at hl
  306. have h₁ : angle (winding n-1)-residual M ≤ 2*Real.pi*n-residual M := by linarith [hc.1]
  307. have h₂ : 2*Real.pi*n-residual M < angle (winding n)-residual M := by linarith [hc.2]
  308. have hp : 0 ≤ 2*Real.pi*n-residual M := by linarith [hl.1]
  309. have hs₁ := sq_le_sq₀ hl.1.le hp |>.2 h₁
  310. have hs₂ := sq_lt_sq₀ hp hu.1.le |>.2 h₂
  311. have hu' := (abs_lt.1 hu.2).2
  312. have hl' := (abs_lt.1 hl.2).1
  313. unfold quad
  314. constructor <;> nlinarith
  315. theorem positive_winding_bound {n : ℕ} (hn : 0 < n) :
  316. 18 ≤ windingDifference n ∧ windingDifference n ≤ 21 := by
  317. let M := winding (n+2)
  318. let K := residual M
  319. let E := fun k : ℕ => (winding k:ℝ)-quad K k
  320. have e (k : ℕ) (hk : 0 < k) (hk' : k ≤ n+2) :
  321. -(1/20) < E k ∧ E k < 1+1/20 :=
  322. rounding hk (winding_monotone hk')
  323. have e₀ := e n hn (by omega)
  324. have e₁ := e (n+1) (by omega) (by omega)
  325. have e₂ := e (n+2) (by omega) (by omega)
  326. have hQ : quad K (n+2)-2*quad K (n+1)+quad K n=2*Real.pi^2 := by
  327. unfold quad; push_cast; ring
  328. have hd : (windingDifference n:ℝ)=2*Real.pi^2+E (n+2)-2*E (n+1)+E n := by
  329. unfold windingDifference
  330. push_cast
  331. dsimp only [E]
  332. linarith
  333. have piLo : (3.14:ℝ)^2 < Real.pi^2 :=
  334. (sq_lt_sq₀ (by norm_num) Real.pi_pos.le).2 Real.pi_gt_d2
  335. have piHi : Real.pi^2 < (3.1416:ℝ)^2 :=
  336. (sq_lt_sq₀ Real.pi_pos.le (by norm_num)).2 Real.pi_lt_d4
  337. have hlo : (17:ℝ) < windingDifference n := by linarith
  338. have hhi : (windingDifference n:ℝ) < 22 := by linarith
  339. have hlo' : (17:ℤ) < windingDifference n := by exact_mod_cast hlo
  340. have hhi' : windingDifference n < (22:ℤ) := by exact_mod_cast hhi
  341. omega
  342. theorem four_of_crossings
  343. (c1 : Crossing 1 17) (c2 : Crossing 2 54) (c3 : Crossing 3 110)
  344. (_c4 : Crossing 4 186) (c5 : Crossing 5 281) (c6 : Crossing 6 396)
  345. (c7 : Crossing 7 532) (c8 : Crossing 8 686) : FourWindingDifferences := by
  346. have w0 : winding 0 = 0 := by simp [winding]
  347. have w1 := winding_unique (by decide) c1
  348. have w2 := winding_unique (by decide) c2
  349. have w3 := winding_unique (by decide) c3
  350. have w5 := winding_unique (by decide) c5
  351. have w6 := winding_unique (by decide) c6
  352. have w7 := winding_unique (by decide) c7
  353. have w8 := winding_unique (by decide) c8
  354. have d0 : windingDifference 0 = 20 := by norm_num [windingDifference,w0,w1,w2]
  355. have d1 : windingDifference 1 = 19 := by norm_num [windingDifference,w1,w2,w3]
  356. have d5 : windingDifference 5 = 21 := by norm_num [windingDifference,w5,w6,w7]
  357. have d6 : windingDifference 6 = 18 := by norm_num [windingDifference,w6,w7,w8]
  358. unfold FourWindingDifferences
  359. ext z
  360. simp only [Set.mem_insert_iff, Set.mem_singleton_iff, Set.mem_range]
  361. constructor
  362. · rintro ⟨n,rfl⟩
  363. by_cases h : n=0
  364. · subst n; omega
  365. · have hd := positive_winding_bound (n := n) (by omega)
  366. omega
  367. · rintro (rfl|rfl|rfl|rfl)
  368. · exact ⟨6,d6⟩
  369. · exact ⟨1,d1⟩
  370. · exact ⟨0,d0⟩
  371. · exact ⟨5,d5⟩
  372. namespace FiniteCertificate
  373. /-- At j = i+2, lower(1/√j) = num(i)/(45045 j⁶ √j).
  374. Horner form in i makes every intermediate coefficient a natural number.
  375. For the upper bound the numerator increases by 3465 = 45045/13. -/
  376. private def num (i : ℕ) :=
  377. 2506894 + i * (7674433 + i * (9792211 + i * (6672237 +
  378. i * (2561559 + i * (525525 + 45045 * i)))))
  379. private def den (i : ℕ) := 45045*(i+2)^6
  380. private def scale : ℕ := 10000000
  381. /-- A fixed number of binary digit steps proposes a square-root enclosure.
  382. The inequalities for each output are checked separately in `checked`. -/
  383. private def root : ℕ → ℕ → ℕ
  384. | 0, _ => 0
  385. | k+1, a => let b := 2 * root k (a/4); if (b+1)^2 ≤ a then b+1 else b
  386. private def grid (i extra : ℕ) := root 24 (((num i+extra)*scale)^2/((den i)^2*(i+2)))
  387. /-- Grid sums for angles j=2 through n+1. The first angle is exactly π/4. -/
  388. private def total (extra correction : ℕ) : ℕ → ℕ
  389. | 0 => 0
  390. | n+1 => total extra correction n + grid n extra + correction
  391. set_option maxRecDepth 200000
  392. set_option maxHeartbeats 20000000
  393. private noncomputable def lower (x : ℝ) := x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - x^11/11
  394. private noncomputable def upper (x : ℝ) := x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - x^11/11 + x^13/13
  395. private theorem atan_enclosure {x : ℝ} (hx : 0 ≤ x) :
  396. lower x ≤ Real.arctan x ∧ Real.arctan x ≤ upper x := by
  397. have h0 : Monotone (fun t : ℝ => Real.arctan t - lower t) := by
  398. apply monotone_of_hasDerivAt_nonneg (f' := fun t => 1 / (1+t^2) - (1 - t^2 + t^4 - t^6 + t^8 - t^10))
  399. · intro t
  400. unfold lower
  401. convert (Real.hasDerivAt_arctan t).sub ((((((hasDerivAt_id t).sub (((hasDerivAt_id t).pow 3).div_const 3)).add (((hasDerivAt_id t).pow 5).div_const 5)).sub (((hasDerivAt_id t).pow 7).div_const 7)).add (((hasDerivAt_id t).pow 9).div_const 9)).sub (((hasDerivAt_id t).pow 11).div_const 11)) using 1 <;> simp
  402. · intro t
  403. change 0 ≤ _
  404. have ht : (0:ℝ) < 1+t^2 := by positivity
  405. field_simp [ht.ne']
  406. nlinarith [sq_nonneg (t^6)]
  407. have h1 : Monotone (fun t : ℝ => upper t - Real.arctan t) := by
  408. apply monotone_of_hasDerivAt_nonneg (f' := fun t => (1 - t^2 + t^4 - t^6 + t^8 - t^10 + t^12) - 1/(1+t^2))
  409. · intro t
  410. unfold upper
  411. convert (((((((hasDerivAt_id t).sub (((hasDerivAt_id t).pow 3).div_const 3)).add (((hasDerivAt_id t).pow 5).div_const 5)).sub (((hasDerivAt_id t).pow 7).div_const 7)).add (((hasDerivAt_id t).pow 9).div_const 9)).sub (((hasDerivAt_id t).pow 11).div_const 11)).add (((hasDerivAt_id t).pow 13).div_const 13)).sub (Real.hasDerivAt_arctan t) using 1 <;> simp
  412. · intro t
  413. change 0 ≤ _
  414. have ht : (0:ℝ) < 1+t^2 := by positivity
  415. field_simp [ht.ne']
  416. nlinarith [sq_nonneg (t^7)]
  417. have l := h0 hx
  418. have u := h1 hx
  419. norm_num [lower,upper] at l u ⊢
  420. constructor <;> linarith
  421. private def checks (i : ℕ) : Prop :=
  422. grid i 0 ^ 2 * ((den i)^2 * (i+2)) ≤ ((num i)*scale)^2 ∧
  423. ((num i+3465)*scale)^2 ≤ (grid i 3465+1)^2 * ((den i)^2 * (i+2))
  424. private instance (i : ℕ) : Decidable (checks i) := by unfold checks; infer_instance
  425. /-- Exact integer comparisons at all 685 indices, checked by Lean kernel reduction. -/
  426. private theorem checked : ∀ i : Fin 685, checks i := by decide +kernel
  427. /-- The squared integer bounds imply the real angle enclosure at scale 10⁷. -/
  428. private theorem angle_step (i : ℕ) (h : i < 685) :
  429. (grid i 0 : ℝ) ≤ scale * Real.arctan (1/Real.sqrt ((i:ℝ)+2)) ∧
  430. scale * Real.arctan (1/Real.sqrt ((i:ℝ)+2)) ≤ (grid i 3465 : ℝ)+1 := by
  431. let r := Real.sqrt ((i:ℝ)+2)
  432. have hr : 0 < r := Real.sqrt_pos.2 (by positivity)
  433. have hr2 : r^2 = (i:ℝ)+2 := Real.sq_sqrt (by positivity)
  434. have hi : (i:ℝ) = r^2-2 := by linarith
  435. have dl : lower (1/r) * ((den i : ℝ)*r) = num i := by
  436. simp only [lower,den,num,Nat.cast_mul,Nat.cast_add,Nat.cast_pow,Nat.cast_ofNat]
  437. rw [hi]; field_simp [hr.ne']; ring
  438. have du : upper (1/r) * ((den i : ℝ)*r) = num i + (3465:ℝ) := by
  439. simp only [upper,den,num,Nat.cast_mul,Nat.cast_add,Nat.cast_pow,Nat.cast_ofNat]
  440. rw [hi]; field_simp [hr.ne']; ring
  441. have hp : 0 < (den i : ℝ)*r := by unfold den; positivity
  442. obtain ⟨a,b⟩ := checked ⟨i,h⟩
  443. have ha : (grid i 0 : ℝ)^2 * ((den i : ℝ)^2 * ((i:ℝ)+2)) ≤
  444. ((num i : ℝ)*scale)^2 := by exact_mod_cast a
  445. have hb : (((num i : ℝ)+3465)*scale)^2 ≤
  446. ((grid i 3465 : ℝ)+1)^2 * ((den i : ℝ)^2 * ((i:ℝ)+2)) := by exact_mod_cast b
  447. rw [← hr2] at ha hb
  448. have ha' : (grid i 0 : ℝ)*((den i : ℝ)*r) ≤ (num i : ℝ)*scale := by
  449. apply (sq_le_sq₀ (by positivity) (by positivity)).1
  450. nlinarith only [ha]
  451. have hb' : ((num i : ℝ)+3465)*scale ≤ ((grid i 3465 : ℝ)+1)*((den i : ℝ)*r) := by
  452. apply (sq_le_sq₀ (by positivity) (by positivity)).1
  453. nlinarith only [hb]
  454. have ht := atan_enclosure (x := 1/r) (by positivity)
  455. change _ ≤ scale * Real.arctan (1/r) ∧ scale * Real.arctan (1/r) ≤ _
  456. constructor
  457. · refine le_trans ?_ (mul_le_mul_of_nonneg_left ht.1 (by positivity))
  458. apply (le_of_mul_le_mul_right · hp)
  459. rw [mul_assoc, dl, mul_comm (scale : ℝ)]
  460. exact ha'
  461. · refine le_trans (mul_le_mul_of_nonneg_left ht.2 (by positivity)) ?_
  462. apply (le_of_mul_le_mul_right · hp)
  463. rw [mul_assoc, du, mul_comm (scale : ℝ)]
  464. exact hb'
  465. private theorem angle_totals (n : ℕ) (hn : n ≤ 685) :
  466. (total 0 0 n : ℝ) ≤ scale*(angle (n+1)-Real.pi/4) ∧
  467. scale*(angle (n+1)-Real.pi/4) ≤ (total 3465 1 n : ℝ) := by
  468. induction n with
  469. | zero => norm_num [total,angle,Real.arctan_one]
  470. | succ n ih =>
  471. obtain ⟨hl,hu⟩ := ih (by omega)
  472. have hstep := angle_step n (by omega)
  473. have hangle : angle (n+1+1) = angle (n+1)+Real.arctan (1/Real.sqrt ((n:ℝ)+2)) := by
  474. rw [angle, Finset.sum_range_succ, ← angle]
  475. rw [show ((↑(n+1) : ℝ)+1) = (n:ℝ)+2 by push_cast; ring]
  476. simp only [total,Nat.cast_add,Nat.cast_one,add_zero,hangle]
  477. constructor <;> linarith [hstep.1,hstep.2]
  478. private theorem crossing_of_checked (n m : ℕ) (hm : 2 ≤ m) (hM : m ≤ 686)
  479. (L U : ℕ)
  480. (hL : total 0 0 (m-1) = L) (hU : total 3465 1 (m-2) = U)
  481. (h1 : 4*U*1000000 < 10000000*(8*n-1)*3141592)
  482. (h2 : 10000000*(8*n-1)*3141593 < 4*L*1000000)
  483. (hn : 0 < n) : Crossing n m := by
  484. have l := (angle_totals (m-1) (by omega)).1
  485. have u := (angle_totals (m-2) (by omega)).2
  486. rw [hL, show m-1+1=m by omega] at l
  487. rw [hU, show m-2+1=m-1 by omega] at u
  488. have one : 1 ≤ 8*n := by omega
  489. have a : 4*(U:ℝ)*1000000 < 10000000*(8*(n:ℝ)-1)*3141592 := by
  490. have hh : 4*(U:ℝ)*1000000 < 10000000*(↑(8*n-1):ℝ)*3141592 := by exact_mod_cast h1
  491. push_cast [one] at hh
  492. exact hh
  493. have b : 10000000*(8*(n:ℝ)-1)*3141593 < 4*(L:ℝ)*1000000 := by
  494. have hh : 10000000*(↑(8*n-1):ℝ)*3141593 < 4*(L:ℝ)*1000000 := by exact_mod_cast h2
  495. push_cast [one] at hh
  496. exact hh
  497. have p : 0 ≤ 8*(n:ℝ)-1 := by
  498. apply sub_nonneg.mpr
  499. exact_mod_cast one
  500. change angle (m-1) ≤ 2*Real.pi*n ∧ 2*Real.pi*n < angle m
  501. norm_num [scale] at l u
  502. constructor
  503. · nlinarith only [a,u,mul_le_mul_of_nonneg_left Real.pi_gt_d6.le p]
  504. · nlinarith only [b,l,mul_le_mul_of_nonneg_left Real.pi_lt_d6.le p]
  505. theorem crossing1 : Crossing 1 17 :=
  506. crossing_of_checked 1 17 (by decide) (by decide) 55806213 53436052
  507. (by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)
  508. theorem crossing2 : Crossing 2 54 :=
  509. crossing_of_checked 2 54 (by decide) (by decide) 119110144 117766912
  510. (by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)
  511. theorem crossing3 : Crossing 3 110 :=
  512. crossing_of_checked 3 110 (by decide) (by decide) 181432814 180491569
  513. (by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)
  514. theorem crossing4 : Crossing 4 186 :=
  515. crossing_of_checked 4 186 (by decide) (by decide) 244179311 243456805
  516. (by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)
  517. theorem crossing5 : Crossing 5 281 :=
  518. crossing_of_checked 5 281 (by decide) (by decide) 306517873 305931544
  519. (by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)
  520. theorem crossing6 : Crossing 6 396 :=
  521. crossing_of_checked 6 396 (by decide) (by decide) 369142287 368649820
  522. (by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)
  523. theorem crossing7 : Crossing 7 532 :=
  524. crossing_of_checked 7 532 (by decide) (by decide) 432369441 431945923
  525. (by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)
  526. theorem crossing8 : Crossing 8 686 :=
  527. crossing_of_checked 8 686 (by decide) (by decide) 494838604 494466907
  528. (by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)
  529. end FiniteCertificate
  530. open FiniteCertificate in
  531. /-- All and only the four values conjectured in OEIS A172164 occur. -/
  532. theorem conjecture : FourWindingDifferences :=
  533. four_of_crossings crossing1 crossing2 crossing3 crossing4 crossing5 crossing6 crossing7 crossing8
  534. #print axioms conjecture
  535. end Theodorus