import Mathlib.Algebra.Order.Roundimport Mathlib.Algebra.ContinuedFractions.Computation.Translationsimport Mathlib.Algebra.ContinuedFractions.ContinuantsRecurrenceimport Mathlib.Analysis.Real.Pi.Boundsimport Mathlib.RingTheory.Real.Irrationalimport Mathlib.Tactic/-! Records for the distance of sqrt(2) + sqrt(n) to the integers (OEIS A309815). -/namespace SqrtTwoRecordsnoncomputable abbrev alpha := Real.sqrt 2theorem alpha_sq : alpha^2 = 2 := Real.sq_sqrt (by norm_num)theorem alpha_bounds : (7/5 : ℝ) < alpha ∧ alpha < 3/2 := byhave h := alpha_sqhave hp : 0 ≤ alpha := Real.sqrt_nonneg _constructor <;> nlinarithdef IsRecord (n : ℕ) (b : ℤ) : Prop :=0 < n ∧ |alpha + Real.sqrt n - b| < 1/2 ∧∀ k : ℕ, 0 < k → k < n → ∀ c : ℤ,|alpha + Real.sqrt n - b| < |alpha + Real.sqrt k - c|/-- Two Pell approximants of comparable sizes have the same slope. -/theorem pell_separation (p q h j : ℤ)(N : |p^2-2*q^2|=1) (N' : |h^2-2*j^2|=1)(Q : 0 < q) (J₁ : q < 2*j) (J₂ : j ≤ 2*q)(Plo : 7*(q:ℝ)/5 ≤ p) (Phi : (p:ℝ) ≤ 3*q/2)(Hlo : 4*(j:ℝ)/3 ≤ h) (Hhi : (h:ℝ) ≤ 3*j/2) :h*q=p*j := bylet T := (p:ℝ)+alpha*qlet S := (h:ℝ)+alpha*jhave A := alpha_boundshave Qr : 0 < (q:ℝ) := by exact_mod_cast Qhave J₁r : (q:ℝ) < 2*j := by exact_mod_cast J₁have J₂r : (j:ℝ) ≤ 2*q := by exact_mod_cast J₂have Jr : 0 < (j:ℝ) := by linarithhave hTlo : 14*(q:ℝ)/5 < T := by dsimp [T]; nlinarith [mul_pos (sub_pos.mpr A.1) Qr]have hThi : T < 3*q := by dsimp [T]; nlinarith [mul_pos (sub_pos.mpr A.2) Qr]have hSlo : 41*(j:ℝ)/15 < S := by dsimp [S]; nlinarith [mul_pos (sub_pos.mpr A.1) Jr]have hShi : S < 3*j := by dsimp [S]; nlinarith [mul_pos (sub_pos.mpr A.2) Jr]have Tpos : 0 < T := by linarithhave Spos : 0 < S := by linarithhave lo : 4*T/9 < S := by linarithhave hi : S < 9*T/4 := by linarithhave key : S^2+T^2 < 2*alpha*S*T := bynlinarith only [mul_pos (sub_pos.mpr lo) (sub_pos.mpr hi),mul_pos (mul_pos (sub_pos.mpr A.1) Spos) Tpos, mul_pos Spos Tpos]let U := (p:ℝ)^2-2*q^2let V := (h:ℝ)^2-2*j^2have hU : |U|=1 := by dsimp [U]; exact_mod_cast Nhave hV : |V|=1 := by dsimp [V]; exact_mod_cast N'have ident : 2*alpha*S*T*((h:ℝ)*q-p*j)=V*T^2-U*S^2 := bydsimp [S,T,U,V]linear_combination ((h:ℝ)*q-j*p)*(2*alpha*j*q+h*q+j*p) * alpha_sqhave hbound : |2*alpha*S*T*((h:ℝ)*q-p*j)| ≤ S^2+T^2 := byrw [ident]calc |V*T^2-U*S^2| ≤ |V*T^2|+|U*S^2| := abs_sub _ __ = _ := by rw [abs_mul,abs_mul,hU,hV,abs_of_nonneg (sq_nonneg _),abs_of_nonneg (sq_nonneg _)]; ringhave positive : 0 < 2*alpha*S*T := by have hp := A.1; positivityrw [abs_mul,abs_of_pos positive] at hboundhave small : |(h:ℝ)*q-p*j| < 1 := byhave L := hbound.trans_lt keyapply (mul_lt_mul_iff_right₀ positive).mpsimpa only [mul_one] using Lhave Z : |h*q-p*j| < 1 := by exact_mod_cast smallhave := abs_lt.mp Zomegatheorem classify_approximation (p q h j : ℤ)(N : |p^2-2*q^2|=1) (hq : 5 ≤ q)(Plo : 7*(q:ℝ)/5 ≤ p) (Phi : (p:ℝ) ≤ 3*q/2)(hj : 0 < j) (J₂ : j ≤ 2*q)(parity : j ≤ q ∨ (Odd h ∧ Even j))(close : |(h:ℝ)/(j:ℝ)-alpha| ≤ 1/(2*(q:ℝ)^2)) :h*q=p*j := byhave Q : (5:ℝ) ≤ q := by exact_mod_cast hqhave Qpos : (0:ℝ) < q := by linarithhave J : (0:ℝ) < j := by exact_mod_cast hjhave J₂r : (j:ℝ) ≤ 2*q := by exact_mod_cast J₂let u := (h:ℝ)/(j:ℝ)have hu : u*j=h := by dsimp [u]; field_simpchange |u-alpha| ≤ _ at closehave h50 : 1/(2*(q:ℝ)^2) ≤ 1/50 := byapply one_div_le_one_div_of_le (by norm_num)nlinarith [sq_nonneg ((q:ℝ)-5)]have e := abs_le.1 (close.trans h50)have αhi : alpha < 29/20 := by nlinarith [alpha_sq,Real.sqrt_nonneg (2:ℝ)]have uL : 4/3 ≤ u := by linarith [alpha_bounds.1,e.1]have uU : u ≤ 3/2 := by linarith [e.2]have hL : 4*(j:ℝ)/3 ≤ h := by nlinarith [mul_le_mul_of_nonneg_right uL J.le]have hU : (h:ℝ) ≤ 3*j/2 := by nlinarith [mul_le_mul_of_nonneg_right uU J.le]have hmult : |u-alpha| *(2*(q:ℝ)^2) ≤ 1 := (le_div_iff₀ (by positivity)).1 closehave gain : |u-alpha| *(u+alpha)*(2*(q:ℝ)^2) < 3 := byhave huP : 0 ≤ u+alpha := by linarith [alpha_bounds.1]nlinarith only [mul_le_mul_of_nonneg_right hmult huP, uU, alpha_bounds.2]let norm := h^2-2*j^2have rewriteNorm : |(norm:ℝ)|=|u-alpha| *(u+alpha)*j^2 := byhave eq' : (norm:ℝ)=(u-alpha)*(u+alpha)*j^2 := bydsimp [norm]; push_castrw [←hu]linear_combination (j:ℝ)^2 * alpha_sqrw [eq',abs_mul,abs_mul,abs_of_nonneg (sq_nonneg (j:ℝ)),abs_of_nonneg (show 0 ≤ u+alpha by linarith [alpha_bounds.1])]have hbound : |(norm:ℝ)| *(2*(q:ℝ)^2) < 3*j^2 := byrw [rewriteNorm]nlinarith only [mul_lt_mul_of_pos_right gain (sq_pos_of_pos J)]have N6 : |norm|<6 := byhave Hj : (j:ℝ)^2 ≤ (2*q)^2 := by gcongrhave h' : |(norm:ℝ)| < 6 := byby_contra! hvnlinarith only [hbound,Hj,mul_le_mul_of_nonneg_right hv (sq_nonneg (q:ℝ))]exact_mod_cast h'have nz : norm ≠ 0 := byintro zhave z' : (norm:ℝ)=0 := by exact_mod_cast zhave zu : u=alpha := byrw [← abs_eq_zero] at z'rw [rewriteNorm] at z'rcases mul_eq_zero.mp z' with h|h· rcases mul_eq_zero.mp h with a|a· exact sub_eq_zero.mp (abs_eq_zero.mp a)· linarith [alpha_bounds.1]· nlinarithhave irr := irrational_sqrt_twoexact irr ⟨(h:ℚ)/j, by push_cast; exact zu⟩have N1 : |norm|=1 := byrcases parity with jle|⟨⟨v,hv⟩, ⟨w,hw⟩⟩· have jle' : (j:ℝ) ≤ q := by exact_mod_cast jlehave hj2 : (j:ℝ)^2 ≤ (q:ℝ)^2 := by gcongrhave h' : |(norm:ℝ)| < 2 := byby_contra! hhnlinarith only [hbound,hj2,mul_le_mul_of_nonneg_right hh (sq_nonneg (q:ℝ)),sq_pos_of_pos Qpos]have h'' : |norm| < 2 := by exact_mod_cast h'have hnpos := abs_pos.mpr nzomega· have evenV : Even (v*(v+1)) := byrcases Int.even_or_odd v with ⟨k,hk⟩|⟨k,hk⟩· exact ⟨k*(v+1),by rw [hk];ring⟩· exact ⟨v*(k+1),by rw [hk];ring⟩obtain ⟨k,hk⟩ := evenVhave eqN : norm=8*(k-w^2)+1 := bydsimp [norm]rw [hv,hw]; nlinarith only [hk]have bounds := abs_lt.mp N6have : norm = 1 := by omegarw [this,abs_one]have J₁ : q < 2*j := byhave Nreal : |(norm:ℝ)|=1 := by exact_mod_cast N1rw [Nreal,one_mul] at hboundby_contra! hnhave hn' : (2:ℝ)*j ≤ q := by exact_mod_cast hnhave hn2 : ((2:ℝ)*j)^2 ≤ (q:ℝ)^2 := by gcongrnlinarith only [hn2,hbound,sq_pos_of_pos J]exact pell_separation p q h j N N1 (by omega) J₁ J₂ Plo Phi hL hUtheorem candidate (p q : ℤ) (N : |p^2-2*q^2|=1)(Plo : 7*(q:ℝ)/5 ≤ p) (Phi : (p:ℝ) ≤ 3*q/2)(size : (p=7 ∧ q=5) ∨ 12 ≤ q) :∃ x : ℕ, 0 < x ∧ |alpha + Real.sqrt x - q| ≤ 1/(2*(q:ℝ)^2) := byrcases size with ⟨rfl,rfl⟩|Q· refine ⟨13,by decide,?_⟩push_casthave A := alpha_sqhave H := Real.sq_sqrt (x := (13:ℝ)) (by norm_num)have hp := Real.sqrt_nonneg (13:ℝ)have hα := Real.sqrt_nonneg (2:ℝ)have a1 : 14/10 ≤ alpha := by nlinarithhave a2 : alpha ≤ 14143/10000 := by nlinarithhave b1 : 36/10 ≤ Real.sqrt 13 := by nlinarithhave b2 : Real.sqrt 13 ≤ 36056/10000 := by nlinarithrw [abs_le]; constructor <;> linarith· let X := q^2+2-2*phave Qr : (12:ℝ) ≤ q := by exact_mod_cast Qhave Qp : 0 < (q:ℝ) := by linarithhave hX : 0 < X := bychange 0 < q^2+2-2*pexact_mod_cast (show 0 < (q:ℝ)^2+2-2*p by nlinarith [mul_pos Qp (show 0 < (q:ℝ)-3 by linarith)])refine ⟨X.toNat,by omega,?_⟩have castX : (X.toNat:ℝ)=(q:ℝ)^2+2-2*p := byrw [show (X.toNat:ℝ)=((X.toNat:ℤ):ℝ) by norm_cast,Int.toNat_of_nonneg hX.le]dsimp [X]; push_cast; ringlet b := Real.sqrt (X.toNat:ℝ)have b2 : b^2=(q:ℝ)^2+2-2*p := by rw [← castX]; exact Real.sq_sqrt (by positivity)have bp : 0 ≤ b := Real.sqrt_nonneg _have bl : (q:ℝ)-2 ≤ b := byapply (sq_le_sq₀ (by linarith) bp).1nlinarith only [b2,Phi,Qr]let H := b+q-alphalet T := (p:ℝ)+alpha*qhave hlo : 2*(q:ℝ)-4 < H := by dsimp [H]; linarith [alpha_bounds.2]have hpos : 0 < H := by linarithhave tlo : 14*(q:ℝ)/5 < T := bydsimp [T]; nlinarith only [Plo,mul_pos (sub_pos.mpr alpha_bounds.1) Qp]have tpos : 0 < T := by linarithhave prod : 4*(q:ℝ)^2 ≤ H*T := byhave hh : 0 ≤ 2*(q:ℝ)-4 := by linarithhave k := mul_le_mul hlo.le tlo.le (by positivity : 0 ≤ 14*(q:ℝ)/5) hpos.lenlinarith only [k,mul_nonneg Qp.le (show 0 ≤ (q:ℝ)-12 by linarith)]have delta : (alpha+b-q)*H = 2*(alpha*q-p) := bydsimp [H]; nlinarith only [b2,alpha_sq]have pell : |(alpha*q-p)*T|=1 := byhave D : (alpha*q-p)*T = -((p:ℝ)^2-2*(q:ℝ)^2) := bydsimp [T]; linear_combination (q:ℝ)^2 * alpha_sqrw [D,abs_neg]; exact_mod_cast Nrw [abs_mul,abs_of_pos tpos] at pellhave delta' := congrArg abs deltarw [abs_mul,abs_of_pos hpos,abs_mul,abs_of_pos (show (0:ℝ)<2 by norm_num)] at delta'have final : |alpha+b-q| * (H*T) = 2 := byrw [←mul_assoc,delta',mul_assoc,pell,mul_one]change |alpha+b-q| ≤ _apply (le_div_iff₀ (by positivity)).2nlinarith only [final,mul_le_mul_of_nonneg_left prod (abs_nonneg (alpha+b-q))]theorem first_hit (p q : ℤ) (N : |p^2-2*q^2|=1)(Plo : 7*(q:ℝ)/5 ≤ p) (Phi : (p:ℝ) ≤ 3*q/2)(size : (p=7 ∧ q=5) ∨ 12 ≤ q) :∃ n : ℕ, ∃ b : ℤ, IsRecord n b ∧ (b^2+2-(n:ℤ))*q = 2*b*p := byclassicalhave hq : 5 ≤ q := by rcases size with ⟨_,rfl⟩|h <;> omegahave Q : (5:ℝ) ≤ q := by exact_mod_cast hqlet E := 1/(2*(q:ℝ)^2)have Epos : 0 < E := by dsimp [E]; positivityhave E50 : E ≤ 1/50 := byapply one_div_le_one_div_of_le (by norm_num)nlinarith [sq_nonneg ((q:ℝ)-5)]obtain ⟨X,hX,hXq⟩ := candidate p q N Plo Phi sizelet hit := fun n : ℕ => 0 < n ∧ ∃ b : ℤ, |alpha+Real.sqrt n-b| ≤ Ehave hhit : ∃ n, hit n := ⟨X,hX,q,hXq⟩let n := Nat.find hhitobtain ⟨hn,b,hb⟩ := (Nat.find_spec hhit : hit n)have hle : n ≤ X := Nat.find_min' hhit (show hit X from ⟨hX,q,hXq⟩)refine ⟨n,b,⟨hn,by linarith,?_⟩,?_⟩· intro k hk hkn chave hno : ¬ hit k := Nat.find_min hhit hknhave hfar : E < |alpha+Real.sqrt k-c| := byby_contra! hexact hno ⟨hk,c,h⟩exact hb.trans_lt hfar· have n1 : (1:ℝ) ≤ n := by exact_mod_cast hnhave u1 : 1 ≤ Real.sqrt (n:ℝ) := byrw [Real.le_sqrt (by norm_num) (by positivity)]; linarithhave bnd := abs_le.1 hbhave b₂ : (2:ℤ) ≤ b := byhave bh : (1:ℝ) < b := by linarith [alpha_bounds.1,bnd.2]have bhZ : (1:ℤ) < b := by exact_mod_cast bhomegahave bp : (0:ℝ) < b := by exact_mod_cast (show 0 < b by omega)have b₂r : (2:ℝ) ≤ b := by exact_mod_cast b₂have bq : b ≤ q := byhave rle := Real.sqrt_le_sqrt (show (n:ℝ) ≤ X by exact_mod_cast hle)have h₁ := (abs_le.1 hXq).2change alpha+Real.sqrt (X:ℝ)-q ≤ E at h₁have hh : (b:ℝ)<(q:ℝ)+1 := by linarith [bnd.1]have hh' : b < q+1 := by exact_mod_cast hhomegalet h : ℤ := b^2+2-(n:ℤ)let r := Real.sqrt (n:ℝ)let f := (r+b-alpha)/(2*b)have rootSq : r^2=(n:ℝ) := Real.sq_sqrt (by positivity)have f0 : 0 ≤ f := bydsimp [f,r]; apply div_nonneg _ (by positivity)linarith [alpha_bounds.2]have f1 : f ≤ 1 := bydsimp [f,r]; apply (div_le_iff₀ (by positivity)).2linarith [bnd.2,alpha_bounds.1]have eqH : (h:ℝ)/(2*b)-alpha = -(alpha+r-b)*f := bydsimp [h,f]; push_castfield_simpnlinarith only [alpha_sq,rootSq]have ch : |(h:ℝ)/(2*b)-alpha| ≤ E := byrw [eqH,abs_mul,abs_neg,abs_of_nonneg f0]exact (mul_le_mul_of_nonneg_left f1 (abs_nonneg _)).trans (by simpa [r] using hb)rcases Int.even_or_odd h with ⟨d,hd⟩|ho· have cd : |(d:ℝ)/b-alpha| ≤ E := byhave hc : (h:ℝ) = d+d := by exact_mod_cast hdrwa [hc,show ((d:ℝ)+d)/(2*b)=(d:ℝ)/b by ring] at chhave H := classify_approximation p q d b N hq Plo Phi (by omega) (by omega) (.inl bq) cdchange h*q=2*b*prw [hd]; linear_combination 2*H· have H := classify_approximation p q h (2*b) N hq Plo Phi (by omega) (by omega)(.inr ⟨ho,⟨b,by ring⟩⟩) (by simpa [E] using ch)change h*q=2*b*plinear_combination Hopen GenContFractdef pellPair : ℕ → ℤ × ℤ| 0 => (1,1)| k+1 => let (p,q) := pellPair k; (p+2*q,p+q)private theorem floor_alpha : ⌊alpha⌋ = 1 := byapply Int.floor_eq_iff.mprconstructor <;> norm_num <;> linarith [alpha_bounds.1,alpha_bounds.2]private theorem partials (k : ℕ) : (GenContFract.of alpha).s.get? k = some ⟨1,2⟩ := byhave h1 : ⌊alpha + 1⌋ = 2 := byapply Int.floor_eq_iff.mprconstructor <;> norm_num <;> linarith [alpha_bounds.1,alpha_bounds.2]have hn : alpha-1 ≠ 0 := by linarith [alpha_bounds.1]have hinv : (alpha-1)⁻¹ = alpha+1 := byfield_simp [hn]nlinarith [alpha_sq]have hfirst : IntFractPair.of alpha = ⟨1,alpha-1⟩ := bysimp [IntFractPair.of,Int.fract,floor_alpha]have htail : IntFractPair.of (alpha-1)⁻¹ = ⟨2,alpha-1⟩ := byrw [hinv]simp only [IntFractPair.of,Int.fract,h1,Int.cast_ofNat]congr 1 <;> ringhave stream (i : ℕ) : IntFractPair.stream alpha i =some ⟨if i=0 then 1 else 2,alpha-1⟩ := byinduction i with| zero => simp [IntFractPair.stream_zero,hfirst]| succ i ih =>rw [IntFractPair.stream_succ_of_some ih hn,htail]simphave h := get?_of_eq_some_of_succ_get?_intFractPair_stream (stream (k+1))simpa using h/-- The integer pair at index k is exactly the usual pair of continued-fractioncontinuants, with the zeroth convergent equal to 1. -/theorem convergent_pair (k : ℕ) : (GenContFract.of alpha).conts k =⟨((pellPair k).1 : ℝ), ((pellPair k).2 : ℝ)⟩ := byinduction k using Nat.twoStepInduction with| zero => simp [zeroth_cont_eq_h_one, of_h_eq_floor, floor_alpha, pellPair]| one => norm_num [first_cont_eq (partials 0),of_h_eq_floor,floor_alpha,pellPair]| more k hk hk1 =>rw [conts_recurrence (partials (k+1)) hk hk1]cases h : pellPair k with| mk p q =>simp [pellPair,h]constructor <;> ringtheorem convergent_value (k : ℕ) : (GenContFract.of alpha).convs k =((pellPair k).1 : ℝ)/((pellPair k).2 : ℝ) := byrw [conv_eq_num_div_den,num_eq_conts_a,den_eq_conts_b,convergent_pair]private theorem pellNorm (k : ℕ) :|(pellPair k).1^2-2*(pellPair k).2^2|=1 := byinduction k with| zero => decide| succ k ih =>cases e : pellPair k with| mk p q =>rw [e] at ihsimp only [pellPair,e]rw [show (p+2*q)^2-2*(p+q)^2 = -(p^2-2*q^2) by ring,abs_neg]exact ihprivate theorem sizes (k : ℕ) :0 < (pellPair k).2 ∧ (pellPair k).2 ≤ (pellPair k).1 ∧2*(pellPair k).1 ≤ 3*(pellPair k).2 := byinduction k with| zero => decide| succ k ih =>cases e : pellPair k with| mk p q =>rw [e] at ihsimp only [pellPair,e]obtain ⟨_,_,_⟩ := ihexact ⟨by omega,by omega,by omega⟩private lemma q_mono : Monotone (fun k => (pellPair k).2) := byapply monotone_nat_of_le_succintro khave h := sizes kcases e : pellPair k with| mk p q =>rw [e] at hchange q ≤ (pellPair (k+1)).2simp only [pellPair,e]obtain ⟨_,_,_⟩ := homegatheorem convergent_data (i : ℕ) :∃ p q : ℤ, (GenContFract.of alpha).convs (i+1) = (p:ℝ)/q ∧|p^2-2*q^2|=1 ∧ 7*(q:ℝ)/5 ≤ p ∧ (p:ℝ) ≤ 3*q/2 ∧((p=3 ∧ q=2) ∨ (p=7 ∧ q=5) ∨ 12 ≤ q) := bylet p := (pellPair (i+1)).1let q := (pellPair (i+1)).2refine ⟨p,q,convergent_value (i+1),pellNorm (i+1),?_,?_,?_⟩· have h := sizes icases e : pellPair i with| mk r s =>rw [e] at hsimp only [p,q,pellPair,e,Int.cast_add,Int.cast_mul,Int.cast_ofNat]have hu : 2*(r:ℝ) ≤ 3*s := by exact_mod_cast h.2.2linarith· have h : 2*(p:ℝ) ≤ 3*q := by exact_mod_cast (sizes (i+1)).2.2linarith· rcases i with _|i· decidercases i with _|i· decideright; rightexact (show (12:ℤ) ≤ (pellPair 3).2 by decide).trans (q_mono (by omega))/-- The first nontrivial convergent, 3/2, occurs at x=2 with nearest integer 3. -/theorem first_record : IsRecord 2 3 := byhave ha := alpha_boundshave ab : |alpha + Real.sqrt (2:ℕ) - (3:ℤ)| = 3-2*alpha := bychange |alpha+alpha-3|=_rw [abs_of_nonpos (by linarith [ha.2])]ringrefine ⟨by decide,?_,?_⟩· rw [ab]linarith [ha.1]· intro k hk hlt chave hk1 : k = 1 := by omegasubst krw [ab]norm_numrcases le_or_gt c 2 with h|h· have h' : (c:ℝ) ≤ 2 := by exact_mod_cast hrw [abs_of_nonneg (by linarith [ha.1])]linarith [ha.1]· have h' : (3:ℝ) ≤ c := by exact_mod_cast (show (3:ℤ) ≤ c by omega)rw [abs_of_nonpos (by linarith [ha.2])]linarith [ha.1]/-- The integer in a record is precisely the integer selected by rounding. -/theorem record_round {n : ℕ} {b : ℤ} (h : IsRecord n b) :round (alpha + Real.sqrt n) = b := byrw [round_eq, Int.floor_eq_iff]obtain ⟨ha,hb⟩ := abs_lt.mp h.2.1constructor <;> linariththeorem record_pos {n : ℕ} {b : ℤ} (h : IsRecord n b) : 0 < b := byhave ha := abs_lt.mp h.2.1have hp : (0:ℝ) < b := by linarith [Real.sqrt_nonneg (n:ℝ),alpha_bounds.1]exact_mod_cast hp/-- The number obtained by rounding sqrt(2)+sqrt(n), then applying Israel's formula. -/noncomputable def approximation (n : ℕ) : ℝ :=let b := (round (alpha+Real.sqrt n) : ℝ)(b^2+2-n)/(2*b)/-- Every positive-index continued-fraction convergent of sqrt(2) arises from a record. -/theorem conjecture (i : ℕ) :∃ n : ℕ, IsRecord n (round (alpha+Real.sqrt n)) ∧approximation n = (GenContFract.of (Real.sqrt 2)).convs (i+1) := byobtain ⟨p,q,hcf,hN,hL,hU,hcases⟩ := convergent_data ihave hrep : ∃ n : ℕ, ∃ b : ℤ, IsRecord n b ∧ (b^2+2-(n:ℤ))*q=2*b*p := byrcases hcases with ⟨rfl,rfl⟩ | hs· exact ⟨2,3,first_record,by decide⟩· exact first_hit p q hN hL hU hsobtain ⟨n,b,rec,eqb⟩ := hrephave rb := record_round recrefine ⟨n,rb ▸ rec,?_⟩have qpos : 0 < q := by rcases hcases with ⟨_,rfl⟩|⟨_,rfl⟩|h <;> omegahave bp := record_pos recunfold approximationrw [rb,hcf]apply (div_eq_div_iff (by positivity) (by positivity)).2have eqreal : ((b:ℝ)^2+2-n)*q = 2*b*p := by exact_mod_cast eqblinarith only [eqreal]theorem one_excluded {n : ℕ} {b : ℤ} (h : IsRecord n b) :b^2 + 2 - (n:ℤ) ≠ 2*b := byintro hehave hn := h.1have eqr : (b:ℝ)^2+2-(n:ℝ)=2*b := by exact_mod_cast hehave hsq := Real.sq_sqrt (Nat.cast_nonneg n : 0 ≤ (n:ℝ))have hbp : 0 < b := record_pos hby_cases h1 : n=1· subst nhave hh : b=1 := bynorm_num at hehave hz : (b-1)*(b-1)=0 := by linarith only [he]rcases mul_eq_zero.mp hz with hz|hz <;> omegasubst bhave ha := abs_lt.mp h.2.1norm_num at halinarith [alpha_bounds.1]· have hs : (b:ℝ)-1 < Real.sqrt n := byby_contra! helnlinarith [mul_nonneg (sub_nonneg.mpr hel) (Real.sqrt_nonneg (n:ℝ)),mul_nonneg (sub_nonneg.mpr hel) (show 0 ≤ (b:ℝ)-1 by exact_mod_cast (show 0 ≤ b-1 by omega))]have older := h.2.2 1 (by decide) (by omega) 2norm_num at olderrw [abs_of_nonneg (by linarith [alpha_bounds.1] : 0 ≤ alpha+1-2)] at olderhave low := le_abs_self (alpha+Real.sqrt n-(b:ℝ))linariththeorem initial_absent {n : ℕ} {b : ℤ} (r : IsRecord n b) : approximation n ≠ 1 := byunfold approximationrw [record_round r]intro badhave bpos := record_pos rhave eq₁ := (div_eq_iff (show (2:ℝ)*b ≠ 0 by positivity)).1 badhave eq₂ : (b:ℝ)^2+2-n = 2*b := by linarith only [eq₁]exact one_excluded r (by exact_mod_cast eq₂)#print axioms conjecture#print axioms initial_absentend SqrtTwoRecords