import Mathlib.Analysis.SpecialFunctions.Trigonometric.ArctanDerivimport Mathlib.Analysis.Real.Pi.Boundsimport Mathlib.Analysis.Calculus.Deriv.MeanValueimport Mathlib.Tactic/-! Analytic estimates for the discrete Theodorus spiral, OEIS A172164. -/namespace Theodorusopen Realnoncomputable def angle (m : ℕ) : ℝ :=∑ j ∈ Finset.range m, Real.arctan (1 / Real.sqrt ((j : ℝ) + 1))def Crossing (n m : ℕ) : Prop :=angle (m - 1) ≤ 2 * Real.pi * n ∧ 2 * Real.pi * n < angle mnoncomputable def winding (n : ℕ) : ℕ :=if n = 0 then 0 else sInf {m : ℕ | 2 * Real.pi * n < angle m}noncomputable def windingDifference (n : ℕ) : ℤ :=(winding (n+2) : ℤ) - 2 * winding (n+1) + winding n/-- Dumortier's published four-value conjecture; index zero here corresponds to OEIS index two. -/def FourWindingDifferences : Prop :=Set.range windingDifference = ({18, 19, 20, 21} : Set ℤ)theorem arctan_bounds {x : ℝ} (hx : 0 ≤ x) :x - x^3/3 ≤ Real.arctan x ∧ Real.arctan x ≤ x - x^3/3 + x^5/5 := byhave hL : Monotone (fun t : ℝ => Real.arctan t - (t - t^3/3)) := byapply monotone_of_hasDerivAt_nonneg (f' := fun t => 1/(1+t^2) - (1-3*t^2/3))· intro tconvert (Real.hasDerivAt_arctan t).sub((hasDerivAt_id t).sub (((hasDerivAt_id t).pow 3).div_const 3)) using 1; simp· intro tchange 0 ≤ _have hd : (0 : ℝ) < 1+t^2 := by positivityfield_simp [hd.ne']nlinarith [sq_nonneg (t^2)]have hU : Monotone (fun t : ℝ => t - t^3/3 + t^5/5 - Real.arctan t) := byapply monotone_of_hasDerivAt_nonneg (f' := fun t => (1-3*t^2/3+5*t^4/5) - 1/(1+t^2))· intro tconvert (((hasDerivAt_id t).sub (((hasDerivAt_id t).pow 3).div_const 3)).add(((hasDerivAt_id t).pow 5).div_const 5)).sub (Real.hasDerivAt_arctan t) using 1; simp· intro tchange 0 ≤ _have hd : (0 : ℝ) < 1+t^2 := by positivityfield_simp [hd.ne']nlinarith [sq_nonneg (t^3)]have L := hL hxhave U := hU hxnorm_num at L Uconstructor <;> linarith/-- Quadratic approximation of a square root with its explicit one-sided remainder. -/theorem sqrt_remainder {z : ℝ} (hz : 0 ≤ z) :0 ≤ 1 + z/2 - Real.sqrt (1+z) ∧1 + z/2 - Real.sqrt (1+z) ≤ z^2/8 := bylet w := Real.sqrt (1+z)have hw : w^2 = 1+z := Real.sq_sqrt (by linarith)have h1 : 1 ≤ w := byhave hp : 0 ≤ w := Real.sqrt_nonneg _nlinarithhave h2 : w ≤ 1+z/2 := by nlinarith [sq_nonneg (w-1)]have hi : (1+z/2-w)*(1+z/2+w) = z^2/4 := by nlinarithchange 0 ≤ 1+z/2-w ∧ 1+z/2-w ≤ z^2/8constructor· linarith· nlinarith [mul_nonneg (sub_nonneg.mpr h2) (show 0 ≤ (1+z/2+w)-2 by linarith)]theorem normalized_sqrt_increment {t : ℝ} (ht : 0 ≤ t) :let S := Real.sqrt (1+7*t/6) + Real.sqrt (1+t/6)1-t/3 ≤ 2/S ∧ 2/S ≤ 1-t/3 + t^2/5 := bylet S := Real.sqrt (1+7*t/6) + Real.sqrt (1+t/6)change 1-t/3 ≤ 2/S ∧ 2/S ≤ 1-t/3 + t^2/5have ha := sqrt_remainder (z := 7*t/6) (by positivity)have hb := sqrt_remainder (z := t/6) (by positivity)have h₁ : 1 ≤ Real.sqrt (1+7*t/6) := byrw [Real.le_sqrt (by norm_num) (by positivity)]; linarithhave h₂ : 1 ≤ Real.sqrt (1+t/6) := byrw [Real.le_sqrt (by norm_num) (by positivity)]; linarithhave hS : 2 ≤ S := by dsimp [S]; linarithhave hSp : 0 < S := by linarithhave hSu : S ≤ 2+2*t/3 := by dsimp [S]; linarith [ha.1,hb.1]have hSl : 2+2*t/3-S ≤ 25*t^2/144 := by dsimp [S]; nlinarith [ha.2,hb.2]constructor· apply (le_div_iff₀ hSp).2nlinarith [mul_nonneg ht (sub_nonneg.mpr hS)]· apply (div_le_iff₀ hSp).2nlinarith [mul_nonneg ht (sub_nonneg.mpr hSu),mul_nonneg (sq_nonneg t) (sub_nonneg.mpr hS),sq_nonneg t]/-- Compare a single angle with a successive difference of 2 sqrt(m+7/6). -/theorem angle_increment {m : ℝ} (hm : 0 < m) :|Real.arctan (1/Real.sqrt m) -(2*Real.sqrt (m+7/6) - 2*Real.sqrt (m+1/6))| ≤(1/Real.sqrt m)^5/5 := bylet u := 1/Real.sqrt mhave hr : 0 < Real.sqrt m := Real.sqrt_pos.2 hmhave hu : 0 < u := one_div_pos.2 hrlet t := u^2let A := Real.sqrt (1+7*t/6)let B := Real.sqrt (1+t/6)have hM : (Real.sqrt m)^2=m := Real.sq_sqrt hm.lehave ht : t*m=1 := by dsimp [t,u]; field_simp [hr.ne']; exact hM.symmhave hA : A^2=1+7*t/6 := Real.sq_sqrt (by dsimp [t]; positivity)have hB : B^2=1+t/6 := Real.sq_sqrt (by dsimp [t]; positivity)have hAp : 0 ≤ A := Real.sqrt_nonneg _have hBp : 0 ≤ B := Real.sqrt_nonneg _have Aeq : Real.sqrt (m+7/6) = Real.sqrt m * A := byapply (sq_eq_sq₀ (Real.sqrt_nonneg _) (mul_nonneg hr.le hAp)).1rw [Real.sq_sqrt (by positivity), mul_pow, hM, hA]; nlinarithhave Beq : Real.sqrt (m+1/6) = Real.sqrt m * B := byapply (sq_eq_sq₀ (Real.sqrt_nonneg _) (mul_nonneg hr.le hBp)).1rw [Real.sq_sqrt (by positivity), mul_pow, hM, hB]; nlinarithhave hpos : 0 < A + B := by dsimp [A,B,t]; positivityhave huR : Real.sqrt m*u=1 := by dsimp [u]; field_simphave hdiff : 2*Real.sqrt (m+7/6)-2*Real.sqrt (m+1/6)= u*(2/(A+B)) := byrw [Aeq,Beq,← mul_div_assoc]apply (eq_div_iff hpos.ne').2calc_ = 2*Real.sqrt m*(A^2-B^2) := by ring_ = 2*Real.sqrt m * u^2 := by rw [hA,hB]; dsimp [t]; ring_ = u*2 := by nlinarith [congrArg (· * u) huR]have hnorm := normalized_sqrt_increment (t := t) (sq_nonneg u)change 1-t/3 ≤ 2/(A+B) ∧ 2/(A+B) ≤ 1-t/3 + t^2/5 at hnormhave hl := mul_le_mul_of_nonneg_left hnorm.1 hu.lehave hh := mul_le_mul_of_nonneg_left hnorm.2 hu.ledsimp [t] at hl hhhave han := arctan_bounds hu.lerw [hdiff]change |Real.arctan u - u*(2/(A+B))| ≤ u^5/5rw [abs_le]constructor <;> nlinarith [han.1,han.2]noncomputable def residual (m : ℕ) : ℝ := angle m - 2 * Real.sqrt ((m:ℝ)+7/6)noncomputable def budget (m : ℕ) : ℝ := (1/Real.sqrt (m:ℝ))^3/5theorem budget_step {m : ℕ} (hm : 0 < m) :(1/Real.sqrt ((m:ℝ)+1))^5/5 ≤ budget m - budget (m+1) := bylet a := Real.sqrt (m:ℝ)let b := Real.sqrt ((m:ℝ)+1)have ha : 0 < a := Real.sqrt_pos.2 (by exact_mod_cast hm)have hb : 0 < b := Real.sqrt_pos.2 (by positivity)have hab : a ≤ b := Real.sqrt_le_sqrt (by linarith)have hA : a^2=(m:ℝ) := Real.sq_sqrt (by positivity)have hB : b^2=(m:ℝ)+1 := Real.sq_sqrt (by positivity)have hDiff : b^5 - a^3*b^2 = b^3*(b^2-a^2)+b^2*a^2*(b-a) := by ringhave h1 : b^2-a^2=1 := by linarithrw [h1,mul_one] at hDiffhave h2 : a^3 ≤ b^3 := by gcongrhave h3 : 0 ≤ b^2*a^2*(b-a) := mul_nonneg (by positivity) (sub_nonneg.mpr hab)unfold budgetpush_castchange (1/b)^5/5 ≤ (1/a)^3/5 - (1/b)^3/5field_simp [ha.ne', hb.ne']nlinariththeorem residual_step {m : ℕ} (hm : 0 < m) :|residual (m+1) - residual m| ≤ budget m - budget (m+1) := byhave h := angle_increment (m := (m:ℝ)+1) (by positivity)have h₂ := budget_step hmapply le_trans _ h₂unfold residual anglerw [Finset.sum_range_succ]push_castconvert h using 1; ring_nftheorem residual_gap {m M : ℕ} (hm : 0 < m) (hM : m ≤ M) :|residual M-residual m| ≤ budget m - budget M := byinduction M, hM using Nat.le_induction with| base => simp| succ M hM ih =>have hs := residual_step (m := M) (by omega)calc |residual (M+1) - residual m|= |(residual (M+1)-residual M)+(residual M-residual m)| := by ring_nf_ ≤ |residual (M+1)-residual M|+|residual M-residual m| := abs_add_le _ __ ≤ _ := by linariththeorem angle_monotone : Monotone angle := byapply monotone_nat_of_le_succintro nsimp only [angle,Finset.sum_range_succ]exact le_add_of_nonneg_right (Real.arctan_nonneg.2 (by positivity))theorem local_angle {m M : ℕ} (hm : 0 < m) (hM : m ≤ M) :|(angle m - residual M)-2*Real.sqrt ((m:ℝ)+7/6)| ≤ budget m := byhave hb : 0 ≤ budget M := by unfold budget; positivityhave h := residual_gap hm hMrw [abs_sub_comm] at hcalc |(angle m - residual M)-2*Real.sqrt ((m:ℝ)+7/6)|= |residual m - residual M| := by unfold residual; ring_nf_ ≤ _ := by linarith/-- Squaring the angle estimate gives an error below 1/20 once m is at least 8. -/theorem squared_angle {m M : ℕ} (hm : 8 ≤ m) (hM : m ≤ M) :0 < angle m - residual M ∧|((angle m - residual M)/2)^2-7/6-m| < 1/20 := bylet a := Real.sqrt (m:ℝ)let b := Real.sqrt ((m:ℝ)+7/6)let d := (angle m - residual M)-2*blet e := budget mhave h8 : (8:ℝ) ≤ m := by exact_mod_cast hmhave ha : 0 < a := Real.sqrt_pos.2 (by linarith)have hA : a^2=(m:ℝ) := Real.sq_sqrt (by positivity)have hB : b^2=(m:ℝ)+7/6 := Real.sq_sqrt (by positivity)have hb : 0 ≤ b := Real.sqrt_nonneg _have ha1 : 1 ≤ a := by nlinarithhave hb1 : 1 ≤ b := by nlinarithhave hab : b ≤ 3*a/2 := by nlinarithhave he : e=1/(5*a^3) := by dsimp [e,budget,a]; ringhave hep : 0 < e := by rw [he]; positivityhave he₁ : e ≤ 1/5 := byrw [he]apply one_div_le_one_div_of_le (by norm_num)nlinarith [show 1^3 ≤ a^3 by gcongr]have hea : e*a=1/(5*(m:ℝ)) := byrw [he,← hA]field_simp [ha.ne']have hem : 1/(5*(m:ℝ)) ≤ 1/40 := byapply one_div_le_one_div_of_le (by norm_num); linarithhave heb : e*b ≤ 3/80 := bynlinarith [mul_le_mul_of_nonneg_left hab hep.le]have hD : |d| ≤ e := local_angle (by omega) hMhave hd := abs_le.1 hDhave hbd : -3/80 ≤ d*b ∧ d*b ≤ 3/80 := byconstructor <;> nlinarith [mul_le_mul_of_nonneg_right hd.1 hb,mul_le_mul_of_nonneg_right hd.2 hb]have hd₁ : 0 ≤ 1/5-d := by linarithhave hd₂ : 0 ≤ 1/5+d := by linarithhave hdd : d^2 ≤ 1/25 := by nlinarith [mul_nonneg hd₁ hd₂]have hrel : angle m-residual M = 2*b+d := by dsimp [d]; ringrw [hrel]constructor· linarith· rw [abs_lt]constructor <;> nlinarith only [hB,hbd.1,hbd.2,hdd,sq_nonneg d]theorem exists_above (x : ℝ) : ∃ m : ℕ, x < angle m := byobtain ⟨N,hN⟩ := exists_nat_gt ((x+|residual 1|+2)^2)refine ⟨N+1, ?_⟩have h := residual_gap (m := 1) (M := N+1) (by decide) (by omega)have hb : budget 1=1/5 := by norm_num [budget]have hp : 0 ≤ budget (N+1) := by unfold budget; positivityhave hdiff := (abs_le.1 h).1rw [hb] at hdiffunfold residual at hdiffhave hs : 0 ≤ Real.sqrt ((N:ℝ)+1+7/6) := Real.sqrt_nonneg _have hsq := Real.sq_sqrt (x := (N:ℝ)+1+7/6) (by positivity)have hbnd : x+|residual 1|+2 < Real.sqrt ((N:ℝ)+1+7/6) := by nlinarithpush_cast at hdiffhave ha := neg_abs_le (residual 1)unfold residual at ha hbndlinariththeorem winding_spec {n : ℕ} (hn : 0 < n) : Crossing n (winding n) := bylet S := {m : ℕ | 2 * Real.pi * n < angle m}have hS : S.Nonempty := exists_above _have hM : winding n=sInf S := by simp [winding,hn.ne',S]have hcross : 2*Real.pi*n < angle (winding n) := byrw [hM]; exact Nat.sInf_mem hShave hpos : 0 < winding n := byby_contra! hrw [Nat.le_zero.1 h] at hcrosssimp [angle] at hcrosshave : (0 : ℝ) ≤ 2*Real.pi*n := by positivitylinarithrefine ⟨?_,hcross⟩by_contra! hhave hle : sInf S ≤ winding n-1 := Nat.sInf_le (show winding n-1 ∈ S from h)omegatheorem winding_unique {m n : ℕ} (hn : 0 < n) (h : Crossing n m) : winding n = m := byhave hw := winding_spec hnrcases lt_trichotomy (winding n) m with hlt|heq|hgt· have hi := angle_monotone (show winding n ≤ m-1 by omega)linarith [h.1,hw.2]· exact heq· have hi := angle_monotone (show m ≤ winding n-1 by omega)linarith [hw.1,h.2]theorem winding_monotone : Monotone winding := byintro n k hnkby_cases h : n=0· simp [h,winding]· have hn : 0 < n := by omegahave hk : 0 < k := by omegahave hN := winding_spec hnhave hK := winding_spec hkby_contra! hbadhave ha := angle_monotone (show winding k ≤ winding n - 1 by omega)have hangle : 2*Real.pi*n ≤ 2*Real.pi*k := by gcongrlinarith [hN.1,hK.2]theorem angle_le (m : ℕ) : angle m ≤ m*(Real.pi/4) := byinduction m with| zero => simp [angle]| succ m ih =>have h : 1 ≤ Real.sqrt ((m:ℝ)+1) := byrw [Real.le_sqrt (by norm_num) (by positivity)]; linarithhave h' : 1/Real.sqrt ((m:ℝ)+1) ≤ 1 := byapply (div_le_iff₀ (by positivity)).2linarithhave ha := Real.arctan_le_arctan h'rw [Real.arctan_one] at haunfold anglerw [Finset.sum_range_succ]unfold angle at ihpush_castlinariththeorem winding_lower {n : ℕ} (hn : 0 < n) : 9 ≤ winding n := byhave hp := Real.pi_poshave hc := (winding_spec hn).2have ha := angle_le (winding n)by_contra! hhave hw : (winding n:ℝ) ≤ 8 := by exact_mod_cast (show winding n ≤ 8 by omega)have hn' : (1:ℝ) ≤ n := by exact_mod_cast hnnlinarith [mul_nonneg (sub_nonneg.mpr hw) hp.le, mul_nonneg (sub_nonneg.mpr hn') hp.le]noncomputable def quad (K : ℝ) (n : ℕ) : ℝ := ((2*Real.pi*n-K)/2)^2-7/6theorem rounding {n M : ℕ} (hn : 0 < n) (hM : winding n ≤ M) :-(1/20) < winding n - quad (residual M) n ∧winding n - quad (residual M) n < 1+1/20 := byhave hw := winding_lower hnhave hc := winding_spec hnhave hu := squared_angle (m := winding n) (by omega) hMhave hl := squared_angle (m := winding n-1) (M := M) (by omega) (by omega)have hcast : ((winding n-1:ℕ):ℝ) = (winding n:ℝ)-1 := byrw [Nat.cast_sub (by omega)]; norm_numrw [hcast] at hlhave h₁ : angle (winding n-1)-residual M ≤ 2*Real.pi*n-residual M := by linarith [hc.1]have h₂ : 2*Real.pi*n-residual M < angle (winding n)-residual M := by linarith [hc.2]have hp : 0 ≤ 2*Real.pi*n-residual M := by linarith [hl.1]have hs₁ := sq_le_sq₀ hl.1.le hp |>.2 h₁have hs₂ := sq_lt_sq₀ hp hu.1.le |>.2 h₂have hu' := (abs_lt.1 hu.2).2have hl' := (abs_lt.1 hl.2).1unfold quadconstructor <;> nlinariththeorem positive_winding_bound {n : ℕ} (hn : 0 < n) :18 ≤ windingDifference n ∧ windingDifference n ≤ 21 := bylet M := winding (n+2)let K := residual Mlet E := fun k : ℕ => (winding k:ℝ)-quad K khave e (k : ℕ) (hk : 0 < k) (hk' : k ≤ n+2) :-(1/20) < E k ∧ E k < 1+1/20 :=rounding hk (winding_monotone hk')have e₀ := e n hn (by omega)have e₁ := e (n+1) (by omega) (by omega)have e₂ := e (n+2) (by omega) (by omega)have hQ : quad K (n+2)-2*quad K (n+1)+quad K n=2*Real.pi^2 := byunfold quad; push_cast; ringhave hd : (windingDifference n:ℝ)=2*Real.pi^2+E (n+2)-2*E (n+1)+E n := byunfold windingDifferencepush_castdsimp only [E]linarithhave piLo : (3.14:ℝ)^2 < Real.pi^2 :=(sq_lt_sq₀ (by norm_num) Real.pi_pos.le).2 Real.pi_gt_d2have piHi : Real.pi^2 < (3.1416:ℝ)^2 :=(sq_lt_sq₀ Real.pi_pos.le (by norm_num)).2 Real.pi_lt_d4have hlo : (17:ℝ) < windingDifference n := by linarithhave hhi : (windingDifference n:ℝ) < 22 := by linarithhave hlo' : (17:ℤ) < windingDifference n := by exact_mod_cast hlohave hhi' : windingDifference n < (22:ℤ) := by exact_mod_cast hhiomegatheorem four_of_crossings(c1 : Crossing 1 17) (c2 : Crossing 2 54) (c3 : Crossing 3 110)(_c4 : Crossing 4 186) (c5 : Crossing 5 281) (c6 : Crossing 6 396)(c7 : Crossing 7 532) (c8 : Crossing 8 686) : FourWindingDifferences := byhave w0 : winding 0 = 0 := by simp [winding]have w1 := winding_unique (by decide) c1have w2 := winding_unique (by decide) c2have w3 := winding_unique (by decide) c3have w5 := winding_unique (by decide) c5have w6 := winding_unique (by decide) c6have w7 := winding_unique (by decide) c7have w8 := winding_unique (by decide) c8have d0 : windingDifference 0 = 20 := by norm_num [windingDifference,w0,w1,w2]have d1 : windingDifference 1 = 19 := by norm_num [windingDifference,w1,w2,w3]have d5 : windingDifference 5 = 21 := by norm_num [windingDifference,w5,w6,w7]have d6 : windingDifference 6 = 18 := by norm_num [windingDifference,w6,w7,w8]unfold FourWindingDifferencesext zsimp only [Set.mem_insert_iff, Set.mem_singleton_iff, Set.mem_range]constructor· rintro ⟨n,rfl⟩by_cases h : n=0· subst n; omega· have hd := positive_winding_bound (n := n) (by omega)omega· rintro (rfl|rfl|rfl|rfl)· exact ⟨6,d6⟩· exact ⟨1,d1⟩· exact ⟨0,d0⟩· exact ⟨5,d5⟩namespace FiniteCertificate/-- At j = i+2, lower(1/√j) = num(i)/(45045 j⁶ √j).Horner form in i makes every intermediate coefficient a natural number.For the upper bound the numerator increases by 3465 = 45045/13. -/private def num (i : ℕ) :=2506894 + i * (7674433 + i * (9792211 + i * (6672237 +i * (2561559 + i * (525525 + 45045 * i)))))private def den (i : ℕ) := 45045*(i+2)^6private def scale : ℕ := 10000000/-- A fixed number of binary digit steps proposes a square-root enclosure.The inequalities for each output are checked separately in `checked`. -/private def root : ℕ → ℕ → ℕ| 0, _ => 0| k+1, a => let b := 2 * root k (a/4); if (b+1)^2 ≤ a then b+1 else bprivate def grid (i extra : ℕ) := root 24 (((num i+extra)*scale)^2/((den i)^2*(i+2)))/-- Grid sums for angles j=2 through n+1. The first angle is exactly π/4. -/private def total (extra correction : ℕ) : ℕ → ℕ| 0 => 0| n+1 => total extra correction n + grid n extra + correctionset_option maxRecDepth 200000set_option maxHeartbeats 20000000private noncomputable def lower (x : ℝ) := x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - x^11/11private noncomputable def upper (x : ℝ) := x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - x^11/11 + x^13/13private theorem atan_enclosure {x : ℝ} (hx : 0 ≤ x) :lower x ≤ Real.arctan x ∧ Real.arctan x ≤ upper x := byhave h0 : Monotone (fun t : ℝ => Real.arctan t - lower t) := byapply monotone_of_hasDerivAt_nonneg (f' := fun t => 1 / (1+t^2) - (1 - t^2 + t^4 - t^6 + t^8 - t^10))· intro tunfold lowerconvert (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· intro tchange 0 ≤ _have ht : (0:ℝ) < 1+t^2 := by positivityfield_simp [ht.ne']nlinarith [sq_nonneg (t^6)]have h1 : Monotone (fun t : ℝ => upper t - Real.arctan t) := byapply monotone_of_hasDerivAt_nonneg (f' := fun t => (1 - t^2 + t^4 - t^6 + t^8 - t^10 + t^12) - 1/(1+t^2))· intro tunfold upperconvert (((((((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· intro tchange 0 ≤ _have ht : (0:ℝ) < 1+t^2 := by positivityfield_simp [ht.ne']nlinarith [sq_nonneg (t^7)]have l := h0 hxhave u := h1 hxnorm_num [lower,upper] at l u ⊢constructor <;> linarithprivate def checks (i : ℕ) : Prop :=grid i 0 ^ 2 * ((den i)^2 * (i+2)) ≤ ((num i)*scale)^2 ∧((num i+3465)*scale)^2 ≤ (grid i 3465+1)^2 * ((den i)^2 * (i+2))private instance (i : ℕ) : Decidable (checks i) := by unfold checks; infer_instance/-- Exact integer comparisons at all 685 indices, checked by Lean kernel reduction. -/private theorem checked : ∀ i : Fin 685, checks i := by decide +kernel/-- The squared integer bounds imply the real angle enclosure at scale 10⁷. -/private theorem angle_step (i : ℕ) (h : i < 685) :(grid i 0 : ℝ) ≤ scale * Real.arctan (1/Real.sqrt ((i:ℝ)+2)) ∧scale * Real.arctan (1/Real.sqrt ((i:ℝ)+2)) ≤ (grid i 3465 : ℝ)+1 := bylet r := Real.sqrt ((i:ℝ)+2)have hr : 0 < r := Real.sqrt_pos.2 (by positivity)have hr2 : r^2 = (i:ℝ)+2 := Real.sq_sqrt (by positivity)have hi : (i:ℝ) = r^2-2 := by linarithhave dl : lower (1/r) * ((den i : ℝ)*r) = num i := bysimp only [lower,den,num,Nat.cast_mul,Nat.cast_add,Nat.cast_pow,Nat.cast_ofNat]rw [hi]; field_simp [hr.ne']; ringhave du : upper (1/r) * ((den i : ℝ)*r) = num i + (3465:ℝ) := bysimp only [upper,den,num,Nat.cast_mul,Nat.cast_add,Nat.cast_pow,Nat.cast_ofNat]rw [hi]; field_simp [hr.ne']; ringhave hp : 0 < (den i : ℝ)*r := by unfold den; positivityobtain ⟨a,b⟩ := checked ⟨i,h⟩have ha : (grid i 0 : ℝ)^2 * ((den i : ℝ)^2 * ((i:ℝ)+2)) ≤((num i : ℝ)*scale)^2 := by exact_mod_cast ahave hb : (((num i : ℝ)+3465)*scale)^2 ≤((grid i 3465 : ℝ)+1)^2 * ((den i : ℝ)^2 * ((i:ℝ)+2)) := by exact_mod_cast brw [← hr2] at ha hbhave ha' : (grid i 0 : ℝ)*((den i : ℝ)*r) ≤ (num i : ℝ)*scale := byapply (sq_le_sq₀ (by positivity) (by positivity)).1nlinarith only [ha]have hb' : ((num i : ℝ)+3465)*scale ≤ ((grid i 3465 : ℝ)+1)*((den i : ℝ)*r) := byapply (sq_le_sq₀ (by positivity) (by positivity)).1nlinarith only [hb]have ht := atan_enclosure (x := 1/r) (by positivity)change _ ≤ scale * Real.arctan (1/r) ∧ scale * Real.arctan (1/r) ≤ _constructor· refine le_trans ?_ (mul_le_mul_of_nonneg_left ht.1 (by positivity))apply (le_of_mul_le_mul_right · hp)rw [mul_assoc, dl, mul_comm (scale : ℝ)]exact ha'· refine le_trans (mul_le_mul_of_nonneg_left ht.2 (by positivity)) ?_apply (le_of_mul_le_mul_right · hp)rw [mul_assoc, du, mul_comm (scale : ℝ)]exact hb'private theorem angle_totals (n : ℕ) (hn : n ≤ 685) :(total 0 0 n : ℝ) ≤ scale*(angle (n+1)-Real.pi/4) ∧scale*(angle (n+1)-Real.pi/4) ≤ (total 3465 1 n : ℝ) := byinduction n with| zero => norm_num [total,angle,Real.arctan_one]| succ n ih =>obtain ⟨hl,hu⟩ := ih (by omega)have hstep := angle_step n (by omega)have hangle : angle (n+1+1) = angle (n+1)+Real.arctan (1/Real.sqrt ((n:ℝ)+2)) := byrw [angle, Finset.sum_range_succ, ← angle]rw [show ((↑(n+1) : ℝ)+1) = (n:ℝ)+2 by push_cast; ring]simp only [total,Nat.cast_add,Nat.cast_one,add_zero,hangle]constructor <;> linarith [hstep.1,hstep.2]private theorem crossing_of_checked (n m : ℕ) (hm : 2 ≤ m) (hM : m ≤ 686)(L U : ℕ)(hL : total 0 0 (m-1) = L) (hU : total 3465 1 (m-2) = U)(h1 : 4*U*1000000 < 10000000*(8*n-1)*3141592)(h2 : 10000000*(8*n-1)*3141593 < 4*L*1000000)(hn : 0 < n) : Crossing n m := byhave l := (angle_totals (m-1) (by omega)).1have u := (angle_totals (m-2) (by omega)).2rw [hL, show m-1+1=m by omega] at lrw [hU, show m-2+1=m-1 by omega] at uhave one : 1 ≤ 8*n := by omegahave a : 4*(U:ℝ)*1000000 < 10000000*(8*(n:ℝ)-1)*3141592 := byhave hh : 4*(U:ℝ)*1000000 < 10000000*(↑(8*n-1):ℝ)*3141592 := by exact_mod_cast h1push_cast [one] at hhexact hhhave b : 10000000*(8*(n:ℝ)-1)*3141593 < 4*(L:ℝ)*1000000 := byhave hh : 10000000*(↑(8*n-1):ℝ)*3141593 < 4*(L:ℝ)*1000000 := by exact_mod_cast h2push_cast [one] at hhexact hhhave p : 0 ≤ 8*(n:ℝ)-1 := byapply sub_nonneg.mprexact_mod_cast onechange angle (m-1) ≤ 2*Real.pi*n ∧ 2*Real.pi*n < angle mnorm_num [scale] at l uconstructor· nlinarith only [a,u,mul_le_mul_of_nonneg_left Real.pi_gt_d6.le p]· nlinarith only [b,l,mul_le_mul_of_nonneg_left Real.pi_lt_d6.le p]theorem crossing1 : Crossing 1 17 :=crossing_of_checked 1 17 (by decide) (by decide) 55806213 53436052(by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)theorem crossing2 : Crossing 2 54 :=crossing_of_checked 2 54 (by decide) (by decide) 119110144 117766912(by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)theorem crossing3 : Crossing 3 110 :=crossing_of_checked 3 110 (by decide) (by decide) 181432814 180491569(by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)theorem crossing4 : Crossing 4 186 :=crossing_of_checked 4 186 (by decide) (by decide) 244179311 243456805(by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)theorem crossing5 : Crossing 5 281 :=crossing_of_checked 5 281 (by decide) (by decide) 306517873 305931544(by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)theorem crossing6 : Crossing 6 396 :=crossing_of_checked 6 396 (by decide) (by decide) 369142287 368649820(by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)theorem crossing7 : Crossing 7 532 :=crossing_of_checked 7 532 (by decide) (by decide) 432369441 431945923(by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)theorem crossing8 : Crossing 8 686 :=crossing_of_checked 8 686 (by decide) (by decide) 494838604 494466907(by decide +kernel) (by decide +kernel) (by decide) (by decide) (by decide)end FiniteCertificateopen FiniteCertificate in/-- All and only the four values conjectured in OEIS A172164 occur. -/theorem conjecture : FourWindingDifferences :=four_of_crossings crossing1 crossing2 crossing3 crossing4 crossing5 crossing6 crossing7 crossing8#print axioms conjectureend Theodorus