Lean / Reader
Lean reader

SqrtTwoRecords.lean

498 lines24 declarationsno sorryimports Mathlib.Algebra.Order.Round Mathlib.Algebra.ContinuedFractions.Computation.Translations Mathlib.Algebra.ContinuedFractions.ContinuantsRecurrence Mathlib.Analysis.Real.Pi.Bounds Mathlib.RingTheory.Real.Irrational Mathlib.Tactic
  1. import Mathlib.Algebra.Order.Round
  2. import Mathlib.Algebra.ContinuedFractions.Computation.Translations
  3. import Mathlib.Algebra.ContinuedFractions.ContinuantsRecurrence
  4. import Mathlib.Analysis.Real.Pi.Bounds
  5. import Mathlib.RingTheory.Real.Irrational
  6. import Mathlib.Tactic
  7. /-! Records for the distance of sqrt(2) + sqrt(n) to the integers (OEIS A309815). -/
  8. namespace SqrtTwoRecords
  9. noncomputable abbrev alpha := Real.sqrt 2
  10. theorem alpha_sq : alpha^2 = 2 := Real.sq_sqrt (by norm_num)
  11. theorem alpha_bounds : (7/5 : ℝ) < alpha ∧ alpha < 3/2 := by
  12. have h := alpha_sq
  13. have hp : 0 ≤ alpha := Real.sqrt_nonneg _
  14. constructor <;> nlinarith
  15. def IsRecord (n : ℕ) (b : ℤ) : Prop :=
  16. 0 < n ∧ |alpha + Real.sqrt n - b| < 1/2 ∧
  17. ∀ k : ℕ, 0 < k → k < n → ∀ c : ℤ,
  18. |alpha + Real.sqrt n - b| < |alpha + Real.sqrt k - c|
  19. /-- Two Pell approximants of comparable sizes have the same slope. -/
  20. theorem pell_separation (p q h j : ℤ)
  21. (N : |p^2-2*q^2|=1) (N' : |h^2-2*j^2|=1)
  22. (Q : 0 < q) (J₁ : q < 2*j) (J₂ : j ≤ 2*q)
  23. (Plo : 7*(q:ℝ)/5 ≤ p) (Phi : (p:ℝ) ≤ 3*q/2)
  24. (Hlo : 4*(j:ℝ)/3 ≤ h) (Hhi : (h:ℝ) ≤ 3*j/2) :
  25. h*q=p*j := by
  26. let T := (p:ℝ)+alpha*q
  27. let S := (h:ℝ)+alpha*j
  28. have A := alpha_bounds
  29. have Qr : 0 < (q:ℝ) := by exact_mod_cast Q
  30. have J₁r : (q:ℝ) < 2*j := by exact_mod_cast J₁
  31. have J₂r : (j:ℝ) ≤ 2*q := by exact_mod_cast J₂
  32. have Jr : 0 < (j:ℝ) := by linarith
  33. have hTlo : 14*(q:ℝ)/5 < T := by dsimp [T]; nlinarith [mul_pos (sub_pos.mpr A.1) Qr]
  34. have hThi : T < 3*q := by dsimp [T]; nlinarith [mul_pos (sub_pos.mpr A.2) Qr]
  35. have hSlo : 41*(j:ℝ)/15 < S := by dsimp [S]; nlinarith [mul_pos (sub_pos.mpr A.1) Jr]
  36. have hShi : S < 3*j := by dsimp [S]; nlinarith [mul_pos (sub_pos.mpr A.2) Jr]
  37. have Tpos : 0 < T := by linarith
  38. have Spos : 0 < S := by linarith
  39. have lo : 4*T/9 < S := by linarith
  40. have hi : S < 9*T/4 := by linarith
  41. have key : S^2+T^2 < 2*alpha*S*T := by
  42. nlinarith only [mul_pos (sub_pos.mpr lo) (sub_pos.mpr hi),
  43. mul_pos (mul_pos (sub_pos.mpr A.1) Spos) Tpos, mul_pos Spos Tpos]
  44. let U := (p:ℝ)^2-2*q^2
  45. let V := (h:ℝ)^2-2*j^2
  46. have hU : |U|=1 := by dsimp [U]; exact_mod_cast N
  47. have hV : |V|=1 := by dsimp [V]; exact_mod_cast N'
  48. have ident : 2*alpha*S*T*((h:ℝ)*q-p*j)=V*T^2-U*S^2 := by
  49. dsimp [S,T,U,V]
  50. linear_combination ((h:ℝ)*q-j*p)*(2*alpha*j*q+h*q+j*p) * alpha_sq
  51. have hbound : |2*alpha*S*T*((h:ℝ)*q-p*j)| ≤ S^2+T^2 := by
  52. rw [ident]
  53. calc |V*T^2-U*S^2| ≤ |V*T^2|+|U*S^2| := abs_sub _ _
  54. _ = _ := by rw [abs_mul,abs_mul,hU,hV,abs_of_nonneg (sq_nonneg _),abs_of_nonneg (sq_nonneg _)]; ring
  55. have positive : 0 < 2*alpha*S*T := by have hp := A.1; positivity
  56. rw [abs_mul,abs_of_pos positive] at hbound
  57. have small : |(h:ℝ)*q-p*j| < 1 := by
  58. have L := hbound.trans_lt key
  59. apply (mul_lt_mul_iff_right₀ positive).mp
  60. simpa only [mul_one] using L
  61. have Z : |h*q-p*j| < 1 := by exact_mod_cast small
  62. have := abs_lt.mp Z
  63. omega
  64. theorem classify_approximation (p q h j : ℤ)
  65. (N : |p^2-2*q^2|=1) (hq : 5 ≤ q)
  66. (Plo : 7*(q:ℝ)/5 ≤ p) (Phi : (p:ℝ) ≤ 3*q/2)
  67. (hj : 0 < j) (J₂ : j ≤ 2*q)
  68. (parity : j ≤ q ∨ (Odd h ∧ Even j))
  69. (close : |(h:ℝ)/(j:ℝ)-alpha| ≤ 1/(2*(q:ℝ)^2)) :
  70. h*q=p*j := by
  71. have Q : (5:ℝ) ≤ q := by exact_mod_cast hq
  72. have Qpos : (0:ℝ) < q := by linarith
  73. have J : (0:ℝ) < j := by exact_mod_cast hj
  74. have J₂r : (j:ℝ) ≤ 2*q := by exact_mod_cast J₂
  75. let u := (h:ℝ)/(j:ℝ)
  76. have hu : u*j=h := by dsimp [u]; field_simp
  77. change |u-alpha| ≤ _ at close
  78. have h50 : 1/(2*(q:ℝ)^2) ≤ 1/50 := by
  79. apply one_div_le_one_div_of_le (by norm_num)
  80. nlinarith [sq_nonneg ((q:ℝ)-5)]
  81. have e := abs_le.1 (close.trans h50)
  82. have αhi : alpha < 29/20 := by nlinarith [alpha_sq,Real.sqrt_nonneg (2:ℝ)]
  83. have uL : 4/3 ≤ u := by linarith [alpha_bounds.1,e.1]
  84. have uU : u ≤ 3/2 := by linarith [e.2]
  85. have hL : 4*(j:ℝ)/3 ≤ h := by nlinarith [mul_le_mul_of_nonneg_right uL J.le]
  86. have hU : (h:ℝ) ≤ 3*j/2 := by nlinarith [mul_le_mul_of_nonneg_right uU J.le]
  87. have hmult : |u-alpha| *(2*(q:ℝ)^2) ≤ 1 := (le_div_iff₀ (by positivity)).1 close
  88. have gain : |u-alpha| *(u+alpha)*(2*(q:ℝ)^2) < 3 := by
  89. have huP : 0 ≤ u+alpha := by linarith [alpha_bounds.1]
  90. nlinarith only [mul_le_mul_of_nonneg_right hmult huP, uU, alpha_bounds.2]
  91. let norm := h^2-2*j^2
  92. have rewriteNorm : |(norm:ℝ)|=|u-alpha| *(u+alpha)*j^2 := by
  93. have eq' : (norm:ℝ)=(u-alpha)*(u+alpha)*j^2 := by
  94. dsimp [norm]; push_cast
  95. rw [←hu]
  96. linear_combination (j:ℝ)^2 * alpha_sq
  97. rw [eq',abs_mul,abs_mul,abs_of_nonneg (sq_nonneg (j:ℝ)),
  98. abs_of_nonneg (show 0 ≤ u+alpha by linarith [alpha_bounds.1])]
  99. have hbound : |(norm:ℝ)| *(2*(q:ℝ)^2) < 3*j^2 := by
  100. rw [rewriteNorm]
  101. nlinarith only [mul_lt_mul_of_pos_right gain (sq_pos_of_pos J)]
  102. have N6 : |norm|<6 := by
  103. have Hj : (j:ℝ)^2 ≤ (2*q)^2 := by gcongr
  104. have h' : |(norm:ℝ)| < 6 := by
  105. by_contra! hv
  106. nlinarith only [hbound,Hj,mul_le_mul_of_nonneg_right hv (sq_nonneg (q:ℝ))]
  107. exact_mod_cast h'
  108. have nz : norm ≠ 0 := by
  109. intro z
  110. have z' : (norm:ℝ)=0 := by exact_mod_cast z
  111. have zu : u=alpha := by
  112. rw [← abs_eq_zero] at z'
  113. rw [rewriteNorm] at z'
  114. rcases mul_eq_zero.mp z' with h|h
  115. · rcases mul_eq_zero.mp h with a|a
  116. · exact sub_eq_zero.mp (abs_eq_zero.mp a)
  117. · linarith [alpha_bounds.1]
  118. · nlinarith
  119. have irr := irrational_sqrt_two
  120. exact irr ⟨(h:ℚ)/j, by push_cast; exact zu⟩
  121. have N1 : |norm|=1 := by
  122. rcases parity with jle|⟨⟨v,hv⟩, ⟨w,hw⟩⟩
  123. · have jle' : (j:ℝ) ≤ q := by exact_mod_cast jle
  124. have hj2 : (j:ℝ)^2 ≤ (q:ℝ)^2 := by gcongr
  125. have h' : |(norm:ℝ)| < 2 := by
  126. by_contra! hh
  127. nlinarith only [hbound,hj2,mul_le_mul_of_nonneg_right hh (sq_nonneg (q:ℝ)),sq_pos_of_pos Qpos]
  128. have h'' : |norm| < 2 := by exact_mod_cast h'
  129. have hnpos := abs_pos.mpr nz
  130. omega
  131. · have evenV : Even (v*(v+1)) := by
  132. rcases Int.even_or_odd v with ⟨k,hk⟩|⟨k,hk⟩
  133. · exact ⟨k*(v+1),by rw [hk];ring⟩
  134. · exact ⟨v*(k+1),by rw [hk];ring⟩
  135. obtain ⟨k,hk⟩ := evenV
  136. have eqN : norm=8*(k-w^2)+1 := by
  137. dsimp [norm]
  138. rw [hv,hw]; nlinarith only [hk]
  139. have bounds := abs_lt.mp N6
  140. have : norm = 1 := by omega
  141. rw [this,abs_one]
  142. have J₁ : q < 2*j := by
  143. have Nreal : |(norm:ℝ)|=1 := by exact_mod_cast N1
  144. rw [Nreal,one_mul] at hbound
  145. by_contra! hn
  146. have hn' : (2:ℝ)*j ≤ q := by exact_mod_cast hn
  147. have hn2 : ((2:ℝ)*j)^2 ≤ (q:ℝ)^2 := by gcongr
  148. nlinarith only [hn2,hbound,sq_pos_of_pos J]
  149. exact pell_separation p q h j N N1 (by omega) J₁ J₂ Plo Phi hL hU
  150. theorem candidate (p q : ℤ) (N : |p^2-2*q^2|=1)
  151. (Plo : 7*(q:ℝ)/5 ≤ p) (Phi : (p:ℝ) ≤ 3*q/2)
  152. (size : (p=7 ∧ q=5) ∨ 12 ≤ q) :
  153. ∃ x : ℕ, 0 < x ∧ |alpha + Real.sqrt x - q| ≤ 1/(2*(q:ℝ)^2) := by
  154. rcases size with ⟨rfl,rfl⟩|Q
  155. · refine ⟨13,by decide,?_⟩
  156. push_cast
  157. have A := alpha_sq
  158. have H := Real.sq_sqrt (x := (13:ℝ)) (by norm_num)
  159. have hp := Real.sqrt_nonneg (13:ℝ)
  160. have hα := Real.sqrt_nonneg (2:ℝ)
  161. have a1 : 14/10 ≤ alpha := by nlinarith
  162. have a2 : alpha ≤ 14143/10000 := by nlinarith
  163. have b1 : 36/10 ≤ Real.sqrt 13 := by nlinarith
  164. have b2 : Real.sqrt 13 ≤ 36056/10000 := by nlinarith
  165. rw [abs_le]; constructor <;> linarith
  166. · let X := q^2+2-2*p
  167. have Qr : (12:ℝ) ≤ q := by exact_mod_cast Q
  168. have Qp : 0 < (q:ℝ) := by linarith
  169. have hX : 0 < X := by
  170. change 0 < q^2+2-2*p
  171. exact_mod_cast (show 0 < (q:ℝ)^2+2-2*p by nlinarith [mul_pos Qp (show 0 < (q:ℝ)-3 by linarith)])
  172. refine ⟨X.toNat,by omega,?_⟩
  173. have castX : (X.toNat:ℝ)=(q:ℝ)^2+2-2*p := by
  174. rw [show (X.toNat:ℝ)=((X.toNat:ℤ):ℝ) by norm_cast,Int.toNat_of_nonneg hX.le]
  175. dsimp [X]; push_cast; ring
  176. let b := Real.sqrt (X.toNat:ℝ)
  177. have b2 : b^2=(q:ℝ)^2+2-2*p := by rw [← castX]; exact Real.sq_sqrt (by positivity)
  178. have bp : 0 ≤ b := Real.sqrt_nonneg _
  179. have bl : (q:ℝ)-2 ≤ b := by
  180. apply (sq_le_sq₀ (by linarith) bp).1
  181. nlinarith only [b2,Phi,Qr]
  182. let H := b+q-alpha
  183. let T := (p:ℝ)+alpha*q
  184. have hlo : 2*(q:ℝ)-4 < H := by dsimp [H]; linarith [alpha_bounds.2]
  185. have hpos : 0 < H := by linarith
  186. have tlo : 14*(q:ℝ)/5 < T := by
  187. dsimp [T]; nlinarith only [Plo,mul_pos (sub_pos.mpr alpha_bounds.1) Qp]
  188. have tpos : 0 < T := by linarith
  189. have prod : 4*(q:ℝ)^2 ≤ H*T := by
  190. have hh : 0 ≤ 2*(q:ℝ)-4 := by linarith
  191. have k := mul_le_mul hlo.le tlo.le (by positivity : 0 ≤ 14*(q:ℝ)/5) hpos.le
  192. nlinarith only [k,mul_nonneg Qp.le (show 0 ≤ (q:ℝ)-12 by linarith)]
  193. have delta : (alpha+b-q)*H = 2*(alpha*q-p) := by
  194. dsimp [H]; nlinarith only [b2,alpha_sq]
  195. have pell : |(alpha*q-p)*T|=1 := by
  196. have D : (alpha*q-p)*T = -((p:ℝ)^2-2*(q:ℝ)^2) := by
  197. dsimp [T]; linear_combination (q:ℝ)^2 * alpha_sq
  198. rw [D,abs_neg]; exact_mod_cast N
  199. rw [abs_mul,abs_of_pos tpos] at pell
  200. have delta' := congrArg abs delta
  201. rw [abs_mul,abs_of_pos hpos,abs_mul,abs_of_pos (show (0:ℝ)<2 by norm_num)] at delta'
  202. have final : |alpha+b-q| * (H*T) = 2 := by
  203. rw [←mul_assoc,delta',mul_assoc,pell,mul_one]
  204. change |alpha+b-q| ≤ _
  205. apply (le_div_iff₀ (by positivity)).2
  206. nlinarith only [final,mul_le_mul_of_nonneg_left prod (abs_nonneg (alpha+b-q))]
  207. theorem first_hit (p q : ℤ) (N : |p^2-2*q^2|=1)
  208. (Plo : 7*(q:ℝ)/5 ≤ p) (Phi : (p:ℝ) ≤ 3*q/2)
  209. (size : (p=7 ∧ q=5) ∨ 12 ≤ q) :
  210. ∃ n : ℕ, ∃ b : ℤ, IsRecord n b ∧ (b^2+2-(n:ℤ))*q = 2*b*p := by
  211. classical
  212. have hq : 5 ≤ q := by rcases size with ⟨_,rfl⟩|h <;> omega
  213. have Q : (5:ℝ) ≤ q := by exact_mod_cast hq
  214. let E := 1/(2*(q:ℝ)^2)
  215. have Epos : 0 < E := by dsimp [E]; positivity
  216. have E50 : E ≤ 1/50 := by
  217. apply one_div_le_one_div_of_le (by norm_num)
  218. nlinarith [sq_nonneg ((q:ℝ)-5)]
  219. obtain ⟨X,hX,hXq⟩ := candidate p q N Plo Phi size
  220. let hit := fun n : ℕ => 0 < n ∧ ∃ b : ℤ, |alpha+Real.sqrt n-b| ≤ E
  221. have hhit : ∃ n, hit n := ⟨X,hX,q,hXq⟩
  222. let n := Nat.find hhit
  223. obtain ⟨hn,b,hb⟩ := (Nat.find_spec hhit : hit n)
  224. have hle : n ≤ X := Nat.find_min' hhit (show hit X from ⟨hX,q,hXq⟩)
  225. refine ⟨n,b,⟨hn,by linarith,?_⟩,?_⟩
  226. · intro k hk hkn c
  227. have hno : ¬ hit k := Nat.find_min hhit hkn
  228. have hfar : E < |alpha+Real.sqrt k-c| := by
  229. by_contra! h
  230. exact hno ⟨hk,c,h⟩
  231. exact hb.trans_lt hfar
  232. · have n1 : (1:ℝ) ≤ n := by exact_mod_cast hn
  233. have u1 : 1 ≤ Real.sqrt (n:ℝ) := by
  234. rw [Real.le_sqrt (by norm_num) (by positivity)]; linarith
  235. have bnd := abs_le.1 hb
  236. have b₂ : (2:ℤ) ≤ b := by
  237. have bh : (1:ℝ) < b := by linarith [alpha_bounds.1,bnd.2]
  238. have bhZ : (1:ℤ) < b := by exact_mod_cast bh
  239. omega
  240. have bp : (0:ℝ) < b := by exact_mod_cast (show 0 < b by omega)
  241. have b₂r : (2:ℝ) ≤ b := by exact_mod_cast b₂
  242. have bq : b ≤ q := by
  243. have rle := Real.sqrt_le_sqrt (show (n:ℝ) ≤ X by exact_mod_cast hle)
  244. have h₁ := (abs_le.1 hXq).2
  245. change alpha+Real.sqrt (X:ℝ)-q ≤ E at h₁
  246. have hh : (b:ℝ)<(q:ℝ)+1 := by linarith [bnd.1]
  247. have hh' : b < q+1 := by exact_mod_cast hh
  248. omega
  249. let h : ℤ := b^2+2-(n:ℤ)
  250. let r := Real.sqrt (n:ℝ)
  251. let f := (r+b-alpha)/(2*b)
  252. have rootSq : r^2=(n:ℝ) := Real.sq_sqrt (by positivity)
  253. have f0 : 0 ≤ f := by
  254. dsimp [f,r]; apply div_nonneg _ (by positivity)
  255. linarith [alpha_bounds.2]
  256. have f1 : f ≤ 1 := by
  257. dsimp [f,r]; apply (div_le_iff₀ (by positivity)).2
  258. linarith [bnd.2,alpha_bounds.1]
  259. have eqH : (h:ℝ)/(2*b)-alpha = -(alpha+r-b)*f := by
  260. dsimp [h,f]; push_cast
  261. field_simp
  262. nlinarith only [alpha_sq,rootSq]
  263. have ch : |(h:ℝ)/(2*b)-alpha| ≤ E := by
  264. rw [eqH,abs_mul,abs_neg,abs_of_nonneg f0]
  265. exact (mul_le_mul_of_nonneg_left f1 (abs_nonneg _)).trans (by simpa [r] using hb)
  266. rcases Int.even_or_odd h with ⟨d,hd⟩|ho
  267. · have cd : |(d:ℝ)/b-alpha| ≤ E := by
  268. have hc : (h:ℝ) = d+d := by exact_mod_cast hd
  269. rwa [hc,show ((d:ℝ)+d)/(2*b)=(d:ℝ)/b by ring] at ch
  270. have H := classify_approximation p q d b N hq Plo Phi (by omega) (by omega) (.inl bq) cd
  271. change h*q=2*b*p
  272. rw [hd]; linear_combination 2*H
  273. · have H := classify_approximation p q h (2*b) N hq Plo Phi (by omega) (by omega)
  274. (.inr ⟨ho,⟨b,by ring⟩⟩) (by simpa [E] using ch)
  275. change h*q=2*b*p
  276. linear_combination H
  277. open GenContFract
  278. def pellPair : ℕ → ℤ × ℤ
  279. | 0 => (1,1)
  280. | k+1 => let (p,q) := pellPair k; (p+2*q,p+q)
  281. private theorem floor_alpha : ⌊alpha⌋ = 1 := by
  282. apply Int.floor_eq_iff.mpr
  283. constructor <;> norm_num <;> linarith [alpha_bounds.1,alpha_bounds.2]
  284. private theorem partials (k : ℕ) : (GenContFract.of alpha).s.get? k = some ⟨1,2⟩ := by
  285. have h1 : ⌊alpha + 1⌋ = 2 := by
  286. apply Int.floor_eq_iff.mpr
  287. constructor <;> norm_num <;> linarith [alpha_bounds.1,alpha_bounds.2]
  288. have hn : alpha-1 ≠ 0 := by linarith [alpha_bounds.1]
  289. have hinv : (alpha-1)⁻¹ = alpha+1 := by
  290. field_simp [hn]
  291. nlinarith [alpha_sq]
  292. have hfirst : IntFractPair.of alpha = ⟨1,alpha-1⟩ := by
  293. simp [IntFractPair.of,Int.fract,floor_alpha]
  294. have htail : IntFractPair.of (alpha-1)⁻¹ = ⟨2,alpha-1⟩ := by
  295. rw [hinv]
  296. simp only [IntFractPair.of,Int.fract,h1,Int.cast_ofNat]
  297. congr 1 <;> ring
  298. have stream (i : ℕ) : IntFractPair.stream alpha i =
  299. some ⟨if i=0 then 1 else 2,alpha-1⟩ := by
  300. induction i with
  301. | zero => simp [IntFractPair.stream_zero,hfirst]
  302. | succ i ih =>
  303. rw [IntFractPair.stream_succ_of_some ih hn,htail]
  304. simp
  305. have h := get?_of_eq_some_of_succ_get?_intFractPair_stream (stream (k+1))
  306. simpa using h
  307. /-- The integer pair at index k is exactly the usual pair of continued-fraction
  308. continuants, with the zeroth convergent equal to 1. -/
  309. theorem convergent_pair (k : ℕ) : (GenContFract.of alpha).conts k =
  310. ⟨((pellPair k).1 : ℝ), ((pellPair k).2 : ℝ)⟩ := by
  311. induction k using Nat.twoStepInduction with
  312. | zero => simp [zeroth_cont_eq_h_one, of_h_eq_floor, floor_alpha, pellPair]
  313. | one => norm_num [first_cont_eq (partials 0),of_h_eq_floor,floor_alpha,pellPair]
  314. | more k hk hk1 =>
  315. rw [conts_recurrence (partials (k+1)) hk hk1]
  316. cases h : pellPair k with
  317. | mk p q =>
  318. simp [pellPair,h]
  319. constructor <;> ring
  320. theorem convergent_value (k : ℕ) : (GenContFract.of alpha).convs k =
  321. ((pellPair k).1 : ℝ)/((pellPair k).2 : ℝ) := by
  322. rw [conv_eq_num_div_den,num_eq_conts_a,den_eq_conts_b,convergent_pair]
  323. private theorem pellNorm (k : ℕ) :
  324. |(pellPair k).1^2-2*(pellPair k).2^2|=1 := by
  325. induction k with
  326. | zero => decide
  327. | succ k ih =>
  328. cases e : pellPair k with
  329. | mk p q =>
  330. rw [e] at ih
  331. simp only [pellPair,e]
  332. rw [show (p+2*q)^2-2*(p+q)^2 = -(p^2-2*q^2) by ring,abs_neg]
  333. exact ih
  334. private theorem sizes (k : ℕ) :
  335. 0 < (pellPair k).2 ∧ (pellPair k).2 ≤ (pellPair k).1 ∧
  336. 2*(pellPair k).1 ≤ 3*(pellPair k).2 := by
  337. induction k with
  338. | zero => decide
  339. | succ k ih =>
  340. cases e : pellPair k with
  341. | mk p q =>
  342. rw [e] at ih
  343. simp only [pellPair,e]
  344. obtain ⟨_,_,_⟩ := ih
  345. exact ⟨by omega,by omega,by omega⟩
  346. private lemma q_mono : Monotone (fun k => (pellPair k).2) := by
  347. apply monotone_nat_of_le_succ
  348. intro k
  349. have h := sizes k
  350. cases e : pellPair k with
  351. | mk p q =>
  352. rw [e] at h
  353. change q ≤ (pellPair (k+1)).2
  354. simp only [pellPair,e]
  355. obtain ⟨_,_,_⟩ := h
  356. omega
  357. theorem convergent_data (i : ℕ) :
  358. ∃ p q : ℤ, (GenContFract.of alpha).convs (i+1) = (p:ℝ)/q ∧
  359. |p^2-2*q^2|=1 ∧ 7*(q:ℝ)/5 ≤ p ∧ (p:ℝ) ≤ 3*q/2 ∧
  360. ((p=3 ∧ q=2) ∨ (p=7 ∧ q=5) ∨ 12 ≤ q) := by
  361. let p := (pellPair (i+1)).1
  362. let q := (pellPair (i+1)).2
  363. refine ⟨p,q,convergent_value (i+1),pellNorm (i+1),?_,?_,?_⟩
  364. · have h := sizes i
  365. cases e : pellPair i with
  366. | mk r s =>
  367. rw [e] at h
  368. simp only [p,q,pellPair,e,Int.cast_add,Int.cast_mul,Int.cast_ofNat]
  369. have hu : 2*(r:ℝ) ≤ 3*s := by exact_mod_cast h.2.2
  370. linarith
  371. · have h : 2*(p:ℝ) ≤ 3*q := by exact_mod_cast (sizes (i+1)).2.2
  372. linarith
  373. · rcases i with _|i
  374. · decide
  375. rcases i with _|i
  376. · decide
  377. right; right
  378. exact (show (12:ℤ) ≤ (pellPair 3).2 by decide).trans (q_mono (by omega))
  379. /-- The first nontrivial convergent, 3/2, occurs at x=2 with nearest integer 3. -/
  380. theorem first_record : IsRecord 2 3 := by
  381. have ha := alpha_bounds
  382. have ab : |alpha + Real.sqrt (2:ℕ) - (3:ℤ)| = 3-2*alpha := by
  383. change |alpha+alpha-3|=_
  384. rw [abs_of_nonpos (by linarith [ha.2])]
  385. ring
  386. refine ⟨by decide,?_,?_⟩
  387. · rw [ab]
  388. linarith [ha.1]
  389. · intro k hk hlt c
  390. have hk1 : k = 1 := by omega
  391. subst k
  392. rw [ab]
  393. norm_num
  394. rcases le_or_gt c 2 with h|h
  395. · have h' : (c:ℝ) ≤ 2 := by exact_mod_cast h
  396. rw [abs_of_nonneg (by linarith [ha.1])]
  397. linarith [ha.1]
  398. · have h' : (3:ℝ) ≤ c := by exact_mod_cast (show (3:ℤ) ≤ c by omega)
  399. rw [abs_of_nonpos (by linarith [ha.2])]
  400. linarith [ha.1]
  401. /-- The integer in a record is precisely the integer selected by rounding. -/
  402. theorem record_round {n : ℕ} {b : ℤ} (h : IsRecord n b) :
  403. round (alpha + Real.sqrt n) = b := by
  404. rw [round_eq, Int.floor_eq_iff]
  405. obtain ⟨ha,hb⟩ := abs_lt.mp h.2.1
  406. constructor <;> linarith
  407. theorem record_pos {n : ℕ} {b : ℤ} (h : IsRecord n b) : 0 < b := by
  408. have ha := abs_lt.mp h.2.1
  409. have hp : (0:ℝ) < b := by linarith [Real.sqrt_nonneg (n:ℝ),alpha_bounds.1]
  410. exact_mod_cast hp
  411. /-- The number obtained by rounding sqrt(2)+sqrt(n), then applying Israel's formula. -/
  412. noncomputable def approximation (n : ℕ) : ℝ :=
  413. let b := (round (alpha+Real.sqrt n) : ℝ)
  414. (b^2+2-n)/(2*b)
  415. /-- Every positive-index continued-fraction convergent of sqrt(2) arises from a record. -/
  416. theorem conjecture (i : ℕ) :
  417. ∃ n : ℕ, IsRecord n (round (alpha+Real.sqrt n)) ∧
  418. approximation n = (GenContFract.of (Real.sqrt 2)).convs (i+1) := by
  419. obtain ⟨p,q,hcf,hN,hL,hU,hcases⟩ := convergent_data i
  420. have hrep : ∃ n : ℕ, ∃ b : ℤ, IsRecord n b ∧ (b^2+2-(n:ℤ))*q=2*b*p := by
  421. rcases hcases with ⟨rfl,rfl⟩ | hs
  422. · exact ⟨2,3,first_record,by decide⟩
  423. · exact first_hit p q hN hL hU hs
  424. obtain ⟨n,b,rec,eqb⟩ := hrep
  425. have rb := record_round rec
  426. refine ⟨n,rb ▸ rec,?_⟩
  427. have qpos : 0 < q := by rcases hcases with ⟨_,rfl⟩|⟨_,rfl⟩|h <;> omega
  428. have bp := record_pos rec
  429. unfold approximation
  430. rw [rb,hcf]
  431. apply (div_eq_div_iff (by positivity) (by positivity)).2
  432. have eqreal : ((b:ℝ)^2+2-n)*q = 2*b*p := by exact_mod_cast eqb
  433. linarith only [eqreal]
  434. theorem one_excluded {n : ℕ} {b : ℤ} (h : IsRecord n b) :
  435. b^2 + 2 - (n:ℤ) ≠ 2*b := by
  436. intro he
  437. have hn := h.1
  438. have eqr : (b:ℝ)^2+2-(n:ℝ)=2*b := by exact_mod_cast he
  439. have hsq := Real.sq_sqrt (Nat.cast_nonneg n : 0 ≤ (n:ℝ))
  440. have hbp : 0 < b := record_pos h
  441. by_cases h1 : n=1
  442. · subst n
  443. have hh : b=1 := by
  444. norm_num at he
  445. have hz : (b-1)*(b-1)=0 := by linarith only [he]
  446. rcases mul_eq_zero.mp hz with hz|hz <;> omega
  447. subst b
  448. have ha := abs_lt.mp h.2.1
  449. norm_num at ha
  450. linarith [alpha_bounds.1]
  451. · have hs : (b:ℝ)-1 < Real.sqrt n := by
  452. by_contra! hel
  453. nlinarith [mul_nonneg (sub_nonneg.mpr hel) (Real.sqrt_nonneg (n:ℝ)),
  454. mul_nonneg (sub_nonneg.mpr hel) (show 0 ≤ (b:ℝ)-1 by exact_mod_cast (show 0 ≤ b-1 by omega))]
  455. have older := h.2.2 1 (by decide) (by omega) 2
  456. norm_num at older
  457. rw [abs_of_nonneg (by linarith [alpha_bounds.1] : 0 ≤ alpha+1-2)] at older
  458. have low := le_abs_self (alpha+Real.sqrt n-(b:ℝ))
  459. linarith
  460. theorem initial_absent {n : ℕ} {b : ℤ} (r : IsRecord n b) : approximation n ≠ 1 := by
  461. unfold approximation
  462. rw [record_round r]
  463. intro bad
  464. have bpos := record_pos r
  465. have eq₁ := (div_eq_iff (show (2:ℝ)*b ≠ 0 by positivity)).1 bad
  466. have eq₂ : (b:ℝ)^2+2-n = 2*b := by linarith only [eq₁]
  467. exact one_excluded r (by exact_mod_cast eq₂)
  468. #print axioms conjecture
  469. #print axioms initial_absent
  470. end SqrtTwoRecords