-- Pell's equation: for a natural d that is not a square, x² = d y² + 1 has -- a solution with y > 0. Among more than 2 (3d)⁴ pairs with |x² − d y²| -- below 3d, two share the value x² − d y² and the residues of x and y -- modulo it; composing them yields a solution. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.NumberTheory.Pell import Grace.Cumulus.NumberTheory.PellApproximants import Grace.Cumulus.Combinatorics.Pigeonhole import Grace.Cumulus.NumberTheory.IntegerDivisibility -- Codes: the distance k = |x² − d y²| in M, the sign in 2 (1 when -- x² ≥ d y²), and the residues x mod k and y mod k in M. def pellCodes (M : Nat) : Set := (M × 2) × (M × M) theorem pellCodes.finite (M : Nat) shows Finite(pellCodes(M)) proof have finiteM := Finite.natural at M have finiteTwo := Finite.natural at 2 have left := Finite.product with finiteM, finiteTwo have right := Finite.product with finiteM, finiteM unfold pellCodes exact Finite.product with left, right theorem pellCodes.cardinality (M : Nat) shows cardinality(pellCodes(M)) = (M * 2) * (M * M) proof have finiteM := Finite.natural at M have finiteTwo := Finite.natural at 2 have left := Finite.product with finiteM, finiteTwo have right := Finite.product with finiteM, finiteM have sizeM := cardinality.natural at M have sizeTwo := cardinality.natural at 2 have leftCount := cardinality.product_count with finiteM, finiteTwo have rightCount := cardinality.product_count with finiteM, finiteM have total := cardinality.product_count with left, right unfold pellCodes rw [total, leftCount, rightCount, sizeM, sizeTwo] -- The distance of a pair, positive for a nonsquare d. def pellDistance (d : Nat, q : Set) : Set := natDist(mul(fst(q), fst(q)), mul(d, mul(snd(q), snd(q)))) theorem pellDistance.value (d x y : Nat) shows pellDistance(d, (x, y)) = natDist(x * x, d * (y * y)) proof unfold pellDistance rw [fst.pair, snd.pair] theorem pellDistance.positive (d M : Nat, q : Set) requires nonsquare : ¬IsSquare(d) requires member : q ∈ pellPairs(d, M) shows 0 < pellDistance(d, q) proof have inProduct := (pellPairs.membership.forward with member).left obtain ⟨_, x, xIn, y, yIn, shape⟩ := product.membership.forward with inProduct rw shape at member have facts := (pellPairs.mem_iff at d, M, x, y).forward with member rw [shape, pellDistance.value] apply (natDist.positive_iff at x * x, d * (y * y)).backward intro square exact nonsquare with (IsSquare.of_square_multiple with facts.left, square) -- The code of a pair: its distance, the sign of x² − d y², and the -- residues of x and y modulo the distance. defgraph pellCode (d M : Nat) (q ∈ pellPairs(d, M)) : pellCodes(M) := ((pellDistance(d, q), chooseNat(isZero(sub(mul(d, mul(snd(q), snd(q))), mul(fst(q), fst(q)))), 1, 0)), (mod(fst(q), pellDistance(d, q)), mod(snd(q), pellDistance(d, q)))) requires nonsquare : ¬IsSquare(d) proof have positive := pellDistance.positive with nonsquare, member have inProduct := (pellPairs.membership.forward with member).left obtain ⟨_, x, xIn, y, yIn, shape⟩ := product.membership.forward with inProduct rw shape at member rw [shape, pellDistance.value] at positive obtain ⟨_, distLess⟩ := (pellPairs.mem_iff at d, M, x, y).forward with member rw [shape, pellDistance.value, fst.pair, snd.pair] have MNat : Nat(M) by typecheck have kNat := natDist.natural at x * x, d * (y * y) have kNonzero : natDist(x * x, d * (y * y)) ≠ 0 by arith have distIn := membership.of_Less with kNat, MNat, distLess have signIn := chooseNat.member_two with (isZero.boolean at d * (y * y) - x * x) have xModNat := mod.natural at natDist(x * x, d * (y * y)), x have yModNat := mod.natural at natDist(x * x, d * (y * y)), y have xModLess := mod.less at x with kNonzero have yModLess := mod.less at y with kNonzero have xModBelow : x % natDist(x * x, d * (y * y)) < M by arith have yModBelow : y % natDist(x * x, d * (y * y)) < M by arith have xModIn := membership.of_Less with xModNat, MNat, xModBelow have yModIn := membership.of_Less with yModNat, MNat, yModBelow unfold pellCodes exact product.pair with (product.pair with distIn, signIn), (product.pair with xModIn, yModIn) -- The sign bit is 1 exactly when d y² ≤ x², and 0 when x² < d y². theorem pellSign.cases (d x y : Nat) shows (chooseNat(isZero(d * (y * y) - x * x), 1, 0) = 1 ∧ d * (y * y) ≤ x * x) ∨ (chooseNat(isZero(d * (y * y) - x * x), 1, 0) = 0 ∧ x * x < d * (y * y)) proof by_cases below : d * (y * y) ≤ x * x . left have zero := sub.eq_zero_of_le with below rw zero simp [isZero, chooseNat] . right have above := Less.of_not_LessEqual with below have positive := sub.positive_of_Less with above have shape := natural.zero_or_successor at d * (y * y) - x * x cases shape with zero | successor . rw zero at positive exfalso arith . obtain ⟨m, mNat, form⟩ := successor rw form simp [isZero, chooseNat] -- Two pairs with the same code have the same signed value x² − d y² = ±k -- and congruent coordinates modulo k. theorem pellCode.same (d M a b c e : Nat) requires nonsquare : ¬IsSquare(d) requires first : (a, b) ∈ pellPairs(d, M) requires second : (c, e) ∈ pellPairs(d, M) requires same : pellCode(d, M)((a, b)) = pellCode(d, M)((c, e)) shows ∃ k, Nat(k) ∧ 0 < k ∧ ((a * a = d * (b * b) + k ∧ c * c = d * (e * e) + k) ∨ (d * (b * b) = a * a + k ∧ d * (e * e) = c * c + k)) ∧ a % k = c % k ∧ b % k = e % k proof have firstValue := pellCode.value at d, M, (a, b) with nonsquare, first have secondValue := pellCode.value at d, M, (c, e) with nonsquare, second rw [firstValue, secondValue] at same rw [pellDistance.value, pellDistance.value] at same rw [fst.pair, fst.pair, snd.pair, snd.pair] at same obtain ⟨outer, inner⟩ := orderedPair.injective with same obtain ⟨sameDist, sameSign⟩ := orderedPair.injective with outer obtain ⟨sameX, sameY⟩ := orderedPair.injective with inner have positive := pellDistance.positive with nonsquare, first rw pellDistance.value at positive rw ← sameDist at sameX rw ← sameDist at sameY witness natDist(a * a, d * (b * b)) refine ⟨positive, ?_, sameX, sameY⟩ have signA := pellSign.cases at d, a, b have signC := pellSign.cases at d, c, e cases signA with aPlus | aMinus . cases signC with cPlus | cMinus . -- Both differences x² − d y² are nonnegative. left have valueA := natDist.of_ge with aPlus.right have valueC := natDist.of_ge with cPlus.right have cancelA := sub.add_cancel_left with aPlus.right have cancelC := sub.add_cancel_left with cPlus.right rw ← valueA at cancelA rw ← valueC at cancelC rw ← sameDist at cancelC constructor . arith . arith . exfalso rw [aPlus.left, cMinus.left] at sameSign arith . cases signC with cPlus | cMinus . exfalso rw [aMinus.left, cPlus.left] at sameSign arith . -- Both differences are negative. right obtain ⟨_, aLess⟩ := aMinus obtain ⟨_, cLess⟩ := cMinus have aBelow : a * a ≤ d * (b * b) by arith have cBelow : c * c ≤ d * (e * e) by arith have valueA := natDist.of_le with aBelow have valueC := natDist.of_le with cBelow have cancelA := sub.add_cancel_left with aBelow have cancelC := sub.add_cancel_left with cBelow rw ← valueA at cancelA rw ← valueC at cancelC rw ← sameDist at cancelC constructor . arith . arith -- Composition. Brahmagupta's identity in ℤ: when C ≡ A and E ≡ B modulo -- K with A² − D B² = C² − D E² = ±K, the composite (A C − D B E, A E − B C) -- is K times a solution of X² − D Y² = 1, whose absolute values solve the -- natural equation; Y ≠ 0 when the cross term A E − B C is nonzero. theorem pell.integer_compose (A B C E K P Q R S sign U V : Integer, d : Nat) requires UDef : U = A * (Q - P) - integerOfNat(d) * (B * (S - R)) + sign requires VDef : V = A * (S - R) - B * (Q - P) requires cDiff : C = A + K * (Q - P) requires eDiff : E = B + K * (S - R) requires first : A * A - integerOfNat(d) * (B * B) = sign * K requires second : C * C - integerOfNat(d) * (E * E) = sign * K requires unit : sign * sign = 1 requires nonzero : K ≠ 0 requires cross : A * E - B * C ≠ 0 shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < y ∧ x * x = d * (y * y) + 1 proof have brahmagupta : (A * C - integerOfNat(d) * (B * E)) * (A * C - integerOfNat(d) * (B * E)) - integerOfNat(d) * ((A * E - B * C) * (A * E - B * C)) = (A * A - integerOfNat(d) * (B * B)) * (C * C - integerOfNat(d) * (E * E)) ring have product : (A * A - integerOfNat(d) * (B * B)) * (C * C - integerOfNat(d) * (E * E)) = K * K rw [first, second] linear_combination K * K * unit have uForm : A * C - integerOfNat(d) * (B * E) = K * U linear_combination A * cDiff - integerOfNat(d) * B * eDiff + first - K * UDef have vForm : A * E - B * C = K * V linear_combination A * eDiff - B * cDiff - K * VDef rw [uForm, vForm, product] at brahmagupta have squareNonzero := integerMulValue.nonzero with nonzero, nonzero have scaled : (K * K) * (U * U - integerOfNat(d) * (V * V)) = (K * K) * integerOne linear_combination brahmagupta have pell := integerMulValue.left_cancel with squareNonzero, scaled -- V ≠ 0, since K V is the cross term. have VNonzero : V ≠ 0 intro VZero rw VZero at vForm apply cross linear_combination vForm have uSquare := Integer.square_natAbs at U have vSquare := Integer.square_natAbs at V symm at uSquare symm at vSquare witness integerNatAbs(U) witness integerNatAbs(V) constructor . have notZero : integerNatAbs(V) ≠ 0 intro zero have VZero := (integerNatAbs.zero_iff at V).forward with zero exact VNonzero with VZero exact Less.positive_of_ne_zero with notZero . have castSolution : integerOfNat(integerNatAbs(U) * integerNatAbs(U)) = integerOfNat(d * (integerNatAbs(V) * integerNatAbs(V)) + 1) rw [integerOfNat.add, integerOfNat.one, uSquare, integerOfNat.mul, vSquare] linear_combination pell exact integerOfNat.injective with castSolution -- A vanishing cross term a e = b c forces the two pairs to coincide: the -- Pell equations give k (e² − b²) = 0, so e = b, and then a = c. theorem pell.equal_of_cross (d a b c e k : Nat) requires positive : 0 < k requires same : (a * a = d * (b * b) + k ∧ c * c = d * (e * e) + k) ∨ (d * (b * b) = a * a + k ∧ d * (e * e) = c * c + k) requires cross : a * e = b * c shows (a, b) = (c, e) proof have kNonzero : k ≠ 0 by arith have kIntNonzero : integerOfNat(k) ≠ 0 intro zero have kZero := (integerOfNat.eq_zero_iff at k).forward with zero exact kNonzero with kZero have castCross : integerOfNat(a * e) = integerOfNat(b * c) rw cross simp [integerOfNat.mul] at castCross -- k (e² − b²) = 0 in ℤ, in either sign case. have scaled : integerOfNat(k) * (integerOfNat(e) * integerOfNat(e) - integerOfNat(b) * integerOfNat(b)) = integerOfNat(k) * 0 cases same with plus | minus . have castA : integerOfNat(a * a) = integerOfNat(d * (b * b) + k) rw plus.left have castC : integerOfNat(c * c) = integerOfNat(d * (e * e) + k) rw plus.right simp [integerOfNat.mul, integerOfNat.add] at castA simp [integerOfNat.mul, integerOfNat.add] at castC linear_combination (integerOfNat(b) * integerOfNat(b)) * castC - (integerOfNat(e) * integerOfNat(e)) * castA + (integerOfNat(a) * integerOfNat(e) + integerOfNat(b) * integerOfNat(c)) * castCross . have castA : integerOfNat(d * (b * b)) = integerOfNat(a * a + k) rw minus.left have castC : integerOfNat(d * (e * e)) = integerOfNat(c * c + k) rw minus.right simp [integerOfNat.mul, integerOfNat.add] at castA simp [integerOfNat.mul, integerOfNat.add] at castC linear_combination (integerOfNat(b) * integerOfNat(b)) * castC - (integerOfNat(e) * integerOfNat(e)) * castA - (integerOfNat(a) * integerOfNat(e) + integerOfNat(b) * integerOfNat(c)) * castCross have difference := integerMulValue.left_cancel with kIntNonzero, scaled have squares : integerOfNat(e) * integerOfNat(e) = integerOfNat(b) * integerOfNat(b) linear_combination difference have eEqual := integerOfNat.eq_of_square_eq with squares rw eEqual at cross rw eEqual at same rw eEqual -- a b = b c: either b = 0, or cancel b. have bShape := natural.zero_or_successor at b cases bShape with bZero | bSucc . rw bZero at same rw bZero cases same with plus | minus . have aSquare : a * a = c * c by arith have castSquare : integerOfNat(a * a) = integerOfNat(c * c) rw aSquare simp [integerOfNat.mul] at castSquare have aEqual := integerOfNat.eq_of_square_eq with castSquare rw aEqual . exfalso obtain ⟨aEquation, _⟩ := minus have zero : d * (0 * 0) = 0 by ring rw zero at aEquation arith . obtain ⟨m, mNat, form⟩ := bSucc have bNonzero : b ≠ 0 rw form arith have swapped : b * a = b * c linear_combination cross have aEqual := mul.left_cancel_positive with bNonzero, swapped rw aEqual -- Composition: k X = a c − d b e and k Y = a e − c b are integers by the -- congruences, X² − d Y² = 1 by linear_combination from the two -- equations, and Y = 0 would force (a, b) = (c, e). theorem pell.compose (d a b c e k : Nat) requires above : 1 < d requires positive : 0 < k requires same : (a * a = d * (b * b) + k ∧ c * c = d * (e * e) + k) ∨ (d * (b * b) = a * a + k ∧ d * (e * e) = c * c + k) requires congruentX : a % k = c % k requires congruentY : b % k = e % k requires distinct : (a, b) ≠ (c, e) shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < y ∧ x * x = d * (y * y) + 1 proof have kNonzero : k ≠ 0 by arith have kIntNonzero : integerOfNat(k) ≠ 0 intro zero have kZero := (integerOfNat.eq_zero_iff at k).forward with zero exact kNonzero with kZero -- The quotients of the four coordinates by k, with shared remainders. obtain ⟨pa, paNat, aShape⟩ := (mod.division at k, a with kNonzero).left obtain ⟨pc, pcNat, cShape⟩ := (mod.division at k, c with kNonzero).left obtain ⟨pb, pbNat, bShape⟩ := (mod.division at k, b with kNonzero).left obtain ⟨pe, peNat, eShape⟩ := (mod.division at k, e with kNonzero).left rw ← congruentX at cShape rw ← congruentY at eShape have castA : integerOfNat(a) = integerOfNat(k * pa + a % k) rw ← aShape have castC : integerOfNat(c) = integerOfNat(k * pc + a % k) rw ← cShape have castB : integerOfNat(b) = integerOfNat(k * pb + b % k) rw ← bShape have castE : integerOfNat(e) = integerOfNat(k * pe + b % k) rw ← eShape simp [integerOfNat.mul, integerOfNat.add] at castA simp [integerOfNat.mul, integerOfNat.add] at castC simp [integerOfNat.mul, integerOfNat.add] at castB simp [integerOfNat.mul, integerOfNat.add] at castE have cDiff : integerOfNat(c) = integerOfNat(a) + integerOfNat(k) * (integerOfNat(pc) - integerOfNat(pa)) linear_combination castC - castA have eDiff : integerOfNat(e) = integerOfNat(b) + integerOfNat(k) * (integerOfNat(pe) - integerOfNat(pb)) linear_combination castE - castB -- The signed Pell equations in ℤ. have signed : ∃ sign ∈ ℤ, sign * sign = 1 ∧ integerOfNat(a) * integerOfNat(a) - integerOfNat(d) * (integerOfNat(b) * integerOfNat(b)) = sign * integerOfNat(k) ∧ integerOfNat(c) * integerOfNat(c) - integerOfNat(d) * (integerOfNat(e) * integerOfNat(e)) = sign * integerOfNat(k) cases same with plus | minus . have castFirst : integerOfNat(a * a) = integerOfNat(d * (b * b) + k) rw plus.left have castSecond : integerOfNat(c * c) = integerOfNat(d * (e * e) + k) rw plus.right simp [integerOfNat.mul, integerOfNat.add] at castFirst simp [integerOfNat.mul, integerOfNat.add] at castSecond have oneIn : integerOne ∈ ℤ by typecheck witness integerOne refine ⟨oneIn, ?_, ?_, ?_⟩ . ring . linear_combination castFirst . linear_combination castSecond . have castFirst : integerOfNat(d * (b * b)) = integerOfNat(a * a + k) rw minus.left have castSecond : integerOfNat(d * (e * e)) = integerOfNat(c * c + k) rw minus.right simp [integerOfNat.mul, integerOfNat.add] at castFirst simp [integerOfNat.mul, integerOfNat.add] at castSecond have negOneIn : -integerOne ∈ ℤ by typecheck witness -integerOne refine ⟨negOneIn, ?_, ?_, ?_⟩ . ring . linear_combination -castFirst . linear_combination -castSecond obtain ⟨sign, signIn, unit, first, second⟩ := signed -- The cross term a e − b c is nonzero, since the pairs differ. have crossNonzero : integerOfNat(a) * integerOfNat(e) - integerOfNat(b) * integerOfNat(c) ≠ 0 intro zero have crossNat : integerOfNat(a * e) = integerOfNat(b * c) rw [integerOfNat.mul, integerOfNat.mul] linear_combination zero have crossEqual := integerOfNat.injective with crossNat have equal := pell.equal_of_cross with positive, same, crossEqual exact distinct with equal -- The composite coordinates. have namedX : ∃ X, Integer(X) ∧ X = integerOfNat(a) * (integerOfNat(pc) - integerOfNat(pa)) - integerOfNat(d) * (integerOfNat(b) * (integerOfNat(pe) - integerOfNat(pb))) + sign witness integerOfNat(a) * (integerOfNat(pc) - integerOfNat(pa)) - integerOfNat(d) * (integerOfNat(b) * (integerOfNat(pe) - integerOfNat(pb))) + sign rfl have namedY : ∃ Y, Integer(Y) ∧ Y = integerOfNat(a) * (integerOfNat(pe) - integerOfNat(pb)) - integerOfNat(b) * (integerOfNat(pc) - integerOfNat(pa)) witness integerOfNat(a) * (integerOfNat(pe) - integerOfNat(pb)) - integerOfNat(b) * (integerOfNat(pc) - integerOfNat(pa)) rfl obtain ⟨X, XInteger, XDef⟩ := namedX obtain ⟨Y, YInteger, YDef⟩ := namedY exact pell.integer_compose with XDef, YDef, cDiff, eDiff, first, second, unit, kIntNonzero, crossNonzero -- The theorem: more pairs than codes, pellPairs.many with n = 2 (3d)⁴ + 1 -- and pigeonhole.collision through pellCode restricted to that set, and -- the composition of the two pairs that share a code. theorem pellCode.collision (d : Nat) requires nonsquare : ¬IsSquare(d) shows ∃ q ∈ pellPairs(d, 3 * d), ∃ r ∈ pellPairs(d, 3 * d), q ≠ r ∧ pellCode(d, 3 * d)(q) = pellCode(d, 3 * d)(r) proof have finiteCodes := pellCodes.finite at 3 * d have count := pellCodes.cardinality at 3 * d obtain ⟨S, subset, counted⟩ := pellPairs.many at d, ((3 * d) * 2) * ((3 * d) * (3 * d)) + 1 with nonsquare have sizeS := cardinality.eq_of_has with counted have function := pellCode.function at d, 3 * d with nonsquare have restricted := restrict.domain_function with function, subset have more : cardinality(pellCodes(3 * d)) < cardinality(S) rw [count, sizeS] arith obtain ⟨q, qIn, r, rIn, different, sameValue⟩ := pigeonhole.collision with restricted, finiteCodes, more have qValue := restrict.domain_apply with function, subset, qIn have rValue := restrict.domain_apply with function, subset, rIn rw [qValue, rValue] at sameValue have qPairs := subset with q, qIn have rPairs := subset with r, rIn witness q refine ⟨qPairs, ?_⟩ witness r exact ⟨rPairs, different, sameValue⟩ theorem pell.exists_nontrivial (d : Nat) requires nonsquare : ¬IsSquare(d) shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < y ∧ x * x = d * (y * y) + 1 proof obtain ⟨q, qIn, r, rIn, different, sameCode⟩ := pellCode.collision with nonsquare have above := IsSquare.above_one_of_not with nonsquare have qProduct := (pellPairs.membership.forward with qIn).left have rProduct := (pellPairs.membership.forward with rIn).left obtain ⟨_, a, aIn, b, bIn, qShape⟩ := product.membership.forward with qProduct obtain ⟨_, c, cIn, e, eIn, rShape⟩ := product.membership.forward with rProduct rw qShape at qIn rw rShape at rIn rw [qShape, rShape] at different rw [qShape, rShape] at sameCode obtain ⟨k, kNat, positive, signs, congruentX, congruentY⟩ := pellCode.same with nonsquare, qIn, rIn, sameCode exact pell.compose with above, positive, signs, congruentX, congruentY, different