Grace/Cumulus/NumberTheory/PellApproximants.g17

Open in Workbench Download source

Imports

Grace.Cumulus.NumberTheory.Dirichlet · Grace.Cumulus.NumberTheory.SqrtNonsquare · Grace.Cumulus.Cardinal

Declarations

Source

1-- Pairs (x, y) with |x² − d y²| below a bound: Dirichlet's approximation2-- of √d gives one with the bound 3d, and for a nonsquare d there are3-- arbitrarily many, since each pair keeps a positive distance |x − y √d|.4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.NumberTheory.PellApproximants10import Grace.Cumulus.NumberTheory.Dirichlet11import Grace.Cumulus.NumberTheory.SqrtNonsquare12import Grace.Cumulus.Cardinal1314-- The pairs of naturals (x, y) with y > 0 and |x² − d y²| < M.1516def pellPairs (d M : Set) : Set :=17  { q ∈ ℕ × ℕ | 0 < snd(q) ∧18    natDist(mul(fst(q), fst(q)), mul(d, mul(snd(q), snd(q)))) < M }1920theorem pellPairs.mem_iff (d M x y : Nat)21  shows (x, y) ∈ pellPairs(d, M) ↔ 0 < y ∧ natDist(x * x, d * (y * y)) < M22proof23  have xIn : x ∈ ℕ by typecheck24  have yIn : y ∈ ℕ by typecheck25  have inProduct := product.pair at x, y, ℕ, ℕ with xIn, yIn26  have first := fst.pair at x, y27  have second := snd.pair at x, y28  have split := pellPairs.membership at d, M, (x, y)29  rw [first, second] at split30  constructor31  . intro member32    have facts := split.forward with member33    exact facts.right34  . intro facts35    apply split.backward36    exact ⟨inProduct, facts⟩3738theorem pellPairs.subset (d M : Nat)39  shows pellPairs(d, M) ⊆ ℕ × ℕ40proof41  intros q member42  have split := pellPairs.membership.forward with member43  exact split.left4445-- The distance in ℝ: x² − d y² = (x − y √d)(x + y √d), and the natural46-- distance is the real absolute value of that difference.4748theorem pellPairs.real_distance (d x y : Nat)49  shows (realNat(natDist(x * x, d * (y * y))):R) =50    abs((realNat(x):R) - (realNat(y):R) * sqrtNat(d)) *51      ((realNat(x):R) + (realNat(y):R) * sqrtNat(d))52proof53  have rootNonneg := sqrtNat.nonnegative at d54  have xNonneg := realNat.nonnegative at x55  have yNonneg := realNat.nonnegative at y56  have square := sqrtNat.square at d57  have productNonneg :=58    realMulValue.nonnegative with yNonneg, rootNonneg59  have expand : (realNat(y) * sqrtNat(d)) * (realNat(y) * sqrtNat(d)) =60      (sqrtNat(d) * sqrtNat(d)) * (realNat(y) * realNat(y))61    ring62  rw square at expand63  have left : realNat(x * x) = realNat(x) * realNat(x)64    exact realNat.mul at x, x65  have right : realNat(d * (y * y)) = realNat(d) * (realNat(y) * realNat(y))66    rw [realNat.mul, realNat.mul]67    rfl68  have total := LessEqual.total at x * x, d * (y * y)69  cases total with below | above70  . -- x² ≤ d y²: the distance is d y² − x², and x ≤ y √d.71    have value := natDist.of_le with below72    have cancel := sub.add_cancel_left with below73    have distNonneg := realNat.nonnegative at d * (y * y) - x * x74    have asReal := realNat.add at x * x, d * (y * y) - x * x75    rw [cancel, left, right] at asReal76    have squares : realNat(x) * realNat(x) ≤77        (realNat(y) * sqrtNat(d)) * (realNat(y) * sqrtNat(d))78      rw expand79      linarith80    have le := realMulValue.le_of_square_le with xNonneg, productNonneg, squares81    have nonpositive : realNat(x) - realNat(y) * sqrtNat(d) ≤ 0 by linarith82    have absValue := abs.of_nonpositive with nonpositive83    rw [value, absValue]84    linear_combination -asReal - (realNat(y) * realNat(y)) * square85  . -- d y² ≤ x²: the distance is x² − d y², and y √d ≤ x.86    have value := natDist.of_ge with above87    have cancel := sub.add_cancel_left with above88    have distNonneg := realNat.nonnegative at x * x - d * (y * y)89    have asReal := realNat.add at d * (y * y), x * x - d * (y * y)90    rw [cancel, left, right] at asReal91    have squares : (realNat(y) * sqrtNat(d)) * (realNat(y) * sqrtNat(d)) ≤92        realNat(x) * realNat(x)93      rw expand94      linarith95    have le := realMulValue.le_of_square_le with productNonneg, xNonneg, squares96    have nonnegative : 0 ≤ realNat(x) - realNat(y) * sqrtNat(d) by linarith97    have absValue := abs.of_nonnegative with nonnegative98    rw [value, absValue]99    linear_combination -asReal + (realNat(y) * realNat(y)) * square100101-- Dirichlet's pair is an approximant: |x − y √d| < 1/N ≤ 1/y gives102-- x < y √d + 1, so |x² − d y²| < (2 y √d + 1) / y ≤ 2 √d + 1 ≤ 3d.103104theorem pellPairs.of_approximation (d x y N : Nat)105  requires above : 1 < d106  requires positive : 0 < y107  requires below : y ≤ N108  requires close : abs((realNat(x):R) - (realNat(y):R) * sqrtNat(d)) *109    (realNat(N):R) < (1:R)110  shows (x, y) ∈ pellPairs(d, 3 * d)111proof112  have realRoot : Real(sqrtNat(d)) by typecheck113  have realDiff : Real(realNat(x) - realNat(y) * sqrtNat(d)) by typecheck114  have realAbs : Real(abs(realNat(x) - realNat(y) * sqrtNat(d))) by typecheck115  have absNonneg := abs.nonnegative with realDiff116  have upper := abs.upper with realDiff117  have dPositive : 0 < d by arith118  have rootPositive := sqrtNat.positive with dPositive119  have rootLe := sqrtNat.le_self with dPositive120  have dAbove := realNat.lt_of_lt at 1, d with above121  rw realNat.one at dAbove122  have yLe := realNat.le_of_natural_le with below123  have oneLeN : 1 ≤ N by arith124  have oneLe := realNat.le_of_natural_le at 1, N with oneLeN125  rw realNat.one at oneLe126  -- |x − y √d| ≤ |x − y √d| N < 1, and likewise times y.127  have scaledOne := realLe.mul_left_of_nonnegative with oneLe, absNonneg128  have unit := realMulValue.one_right with realAbs129  rw unit at scaledOne130  have scaledY := realLe.mul_left_of_nonnegative with yLe, absNonneg131  have absLess : abs(realNat(x) - realNat(y) * sqrtNat(d)) < 1 by linarith132  have absYLess : abs(realNat(x) - realNat(y) * sqrtNat(d)) * realNat(y) < 1133    linarith134  -- x + y √d < 1 + 2 y √d, so the product is below 1 + 2 √d ≤ 1 + 2 d.135  have sumLess : realNat(x) + realNat(y) * sqrtNat(d) <136      1 + 2 * (realNat(y) * sqrtNat(d))137    linarith138  have sumLe := realLess.implies_le with sumLess139  have scaledSum := realLe.mul_left_of_nonnegative with sumLe, absNonneg140  have regroup : abs(realNat(x) - realNat(y) * sqrtNat(d)) *141      (1 + 2 * (realNat(y) * sqrtNat(d))) =142      abs(realNat(x) - realNat(y) * sqrtNat(d)) +143        2 * ((abs(realNat(x) - realNat(y) * sqrtNat(d)) * realNat(y)) *144          sqrtNat(d))145    ring146  have scaledRoot := realLess.mul_right_of_positive with absYLess, rootPositive147  have unitRoot := realMulValue.one_left with realRoot148  rw unitRoot at scaledRoot149  have distance := pellPairs.real_distance at d, x, y150  have three : realNat(3 * d) = (3:R) * realNat(d)151    rw [realNat.mul, realNat.succ, realNat.succ, realNat.one]152    ring153  have realLess : realNat(natDist(x * x, d * (y * y))) < realNat(3 * d)154    rw [distance, three]155    linarith156  have natLess := realNat.lt_of_real_lt with realLess157  have memberIff := pellPairs.mem_iff at d, 3 * d, x, y158  apply memberIff.backward159  exact ⟨positive, natLess⟩160161-- Arbitrarily many approximants: each pair with y > 0 keeps a positive162-- distance from the line x = y √d, so a finite set of pairs has a common163-- gap ε, and Dirichlet's pair for an N with 1/N < ε is a new pair.164165theorem pellPairs.gap (d x y : Nat)166  requires nonsquare : ¬IsSquare(d)167  requires positive : 0 < y168  shows (0:R) < abs((realNat(x):R) - (realNat(y):R) * sqrtNat(d))169proof170  have distinct := sqrtNat.irrational at d, x, y with nonsquare, positive171  have nonzero : realNat(x) - realNat(y) * sqrtNat(d) ≠ 0172    intro zero173    have equal : realNat(x) = realNat(y) * sqrtNat(d) by linear_combination zero174    exact distinct with equal175  exact abs.positive_of_ne with nonzero176177-- Induction on n with HasCardinality.insert_fresh: the new pair's own178-- distance is the next gap.179theorem pellPairs.many_gap (d n : Nat)180  requires nonsquare : ¬IsSquare(d)181  shows ∃ S, S ⊆ pellPairs(d, 3 * d) ∧ HasCardinality(S, n) ∧182    (∃ ε ∈ ℝ, (0:R) < (ε:R) ∧ (∀ q ∈ S,183      (ε:R) ≤ abs(realNat(fst(q)) - realNat(snd(q)) * sqrtNat(d))))184proof185  induction n186  | zero =>187    have emptySubset := empty.subset at pellPairs(d, 3 * d)188    have emptyCounted := HasCardinality.empty_set189    have oneIn : realOne ∈ ℝ by typecheck190    have onePositive := realZero.lt_one191    witness ∅192    refine ⟨emptySubset, emptyCounted, ?_⟩193    witness realOne194    refine ⟨oneIn, onePositive, ?_⟩195    intros q member196    exfalso197    exact set.empty with member198  | succ(k) using ih =>199    obtain ⟨S, subset, counted, ε, εIn, εPositive, bound⟩ := ih200    have above := IsSquare.above_one_of_not with nonsquare201    have rootNonneg := sqrtNat.nonnegative at d202    -- A natural N with 1/ε < N, so that 1 < ε N.203    have εNonzero : ε ≠ 0204      intro zero205      rw zero at εPositive206      linarith207    have inversePositive := realInvValue.positive with εPositive208    obtain ⟨N, NNat, NBig⟩ := Real.below_natural at realInvValue(ε)209    have NNonneg := realNat.nonnegative at N210    have NNonzero : N ≠ 0211      intro zero212      rw [zero, realNat.zero] at NBig213      linarith214    have NPositive := Less.positive_of_ne_zero with NNonzero215    have unit := realInvValue.mul_right with εNonzero216    have scaled := realLess.mul_left_of_positive with NBig, εPositive217    rw unit at scaled218    -- Dirichlet's pair for N is within 1/N < ε of the line.219    obtain ⟨x, xNat, y, yNat, yPositive, yLe, close⟩ :=220      dirichlet.approximation at sqrtNat(d), N with rootNonneg, NPositive221    have realAbs : Real(abs(realNat(x) - realNat(y) * sqrtNat(d))) by typecheck222    have absIn : abs(realNat(x) - realNat(y) * sqrtNat(d)) ∈ ℝ by typecheck223    have gapLess : abs(realNat(x) - realNat(y) * sqrtNat(d)) < ε224      have notLe : ¬(ε ≤ abs(realNat(x) - realNat(y) * sqrtNat(d)))225        intro le226        have scaledLe := realLe.mul_right_of_nonnegative with le, NNonneg227        linarith228      exact realLess.of_not_le with notLe229    have member := pellPairs.of_approximation at d, x, y, N230      with above, yPositive, yLe, close231    have gapPositive := pellPairs.gap at d, x, y with nonsquare, yPositive232    have fresh : ¬((x, y) ∈ S)233      intro inS234      have atPair := bound with (x, y), inS235      rw [fst.pair, snd.pair] at atPair236      linarith237    have countedMore := HasCardinality.insert_fresh with counted, fresh238    witness insert((x, y), S)239    refine ⟨?_, countedMore, ?_⟩240    . intros q qIn241      have split := insert.membership.forward with qIn242      cases split with same | old243      . rw same244        exact member245      . exact subset with q, old246    . witness abs(realNat(x) - realNat(y) * sqrtNat(d))247      refine ⟨absIn, gapPositive, ?_⟩248      intros q qIn249      have split := insert.membership.forward with qIn250      cases split with same | old251      . rw [same, fst.pair, snd.pair]252        exact realLe.reflexive with realAbs253      . have oldGap := bound with q, old254        have inPairs := subset with q, old255        have inProduct := (pellPairs.membership.forward with inPairs).left256        obtain ⟨_, a, aIn, b, bIn, shape⟩ :=257          product.membership.forward with inProduct258        rw [shape, fst.pair, snd.pair] at oldGap259        rw [shape, fst.pair, snd.pair]260        linarith261262theorem pellPairs.many (d n : Nat)263  requires nonsquare : ¬IsSquare(d)264  shows ∃ S, S ⊆ pellPairs(d, 3 * d) ∧ HasCardinality(S, n)265proof266  obtain ⟨S, subset, counted, _⟩ :=267    pellPairs.many_gap at d, n with nonsquare268  witness S269  exact ⟨subset, counted⟩270