Grace/Cumulus/NumberTheory/Pell.g17
Imports
Grace.Cumulus.NumberTheory.PellApproximants · Grace.Cumulus.Combinatorics.Pigeonhole · Grace.Cumulus.NumberTheory.IntegerDivisibility
Declarations
- pellCodes def
- pellCodes.finite theorem derivation
- pellCodes.cardinality theorem derivation
- pellDistance def
- pellDistance.value theorem derivation
- pellDistance.positive theorem derivation
- pellCode defgraph
- pellSign.cases theorem derivation
- pellCode.same theorem derivation
- pell.integer_compose theorem derivation
- pell.equal_of_cross theorem derivation
- pell.compose theorem derivation
- pellCode.collision theorem derivation
- pell.exists_nontrivial theorem derivation
Source
1-- Pell's equation: for a natural d that is not a square, x² = d y² + 1 has2-- a solution with y > 0. Among more than 2 (3d)⁴ pairs with |x² − d y²|3-- below 3d, two share the value x² − d y² and the residues of x and y4-- modulo it; composing them yields a solution.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.NumberTheory.Pell11import Grace.Cumulus.NumberTheory.PellApproximants12import Grace.Cumulus.Combinatorics.Pigeonhole13import Grace.Cumulus.NumberTheory.IntegerDivisibility1415-- Codes: the distance k = |x² − d y²| in M, the sign in 2 (1 when16-- x² ≥ d y²), and the residues x mod k and y mod k in M.1718def pellCodes (M : Nat) : Set := (M × 2) × (M × M)1920theorem pellCodes.finite (M : Nat)21 shows Finite(pellCodes(M))22proof23 have finiteM := Finite.natural at M24 have finiteTwo := Finite.natural at 225 have left := Finite.product with finiteM, finiteTwo26 have right := Finite.product with finiteM, finiteM27 unfold pellCodes28 exact Finite.product with left, right2930theorem pellCodes.cardinality (M : Nat)31 shows cardinality(pellCodes(M)) = (M * 2) * (M * M)32proof33 have finiteM := Finite.natural at M34 have finiteTwo := Finite.natural at 235 have left := Finite.product with finiteM, finiteTwo36 have right := Finite.product with finiteM, finiteM37 have sizeM := cardinality.natural at M38 have sizeTwo := cardinality.natural at 239 have leftCount := cardinality.product_count with finiteM, finiteTwo40 have rightCount := cardinality.product_count with finiteM, finiteM41 have total := cardinality.product_count with left, right42 unfold pellCodes43 rw [total, leftCount, rightCount, sizeM, sizeTwo]4445-- The distance of a pair, positive for a nonsquare d.4647def pellDistance (d : Nat, q : Set) : Set :=48 natDist(mul(fst(q), fst(q)), mul(d, mul(snd(q), snd(q))))4950theorem pellDistance.value (d x y : Nat)51 shows pellDistance(d, (x, y)) = natDist(x * x, d * (y * y))52proof53 unfold pellDistance54 rw [fst.pair, snd.pair]5556theorem pellDistance.positive (d M : Nat, q : Set)57 requires nonsquare : ¬IsSquare(d)58 requires member : q ∈ pellPairs(d, M)59 shows 0 < pellDistance(d, q)60proof61 have inProduct := (pellPairs.membership.forward with member).left62 obtain ⟨_, x, xIn, y, yIn, shape⟩ :=63 product.membership.forward with inProduct64 rw shape at member65 have facts := (pellPairs.mem_iff at d, M, x, y).forward with member66 rw [shape, pellDistance.value]67 apply (natDist.positive_iff at x * x, d * (y * y)).backward68 intro square69 exact nonsquare with (IsSquare.of_square_multiple with facts.left, square)7071-- The code of a pair: its distance, the sign of x² − d y², and the72-- residues of x and y modulo the distance.7374defgraph pellCode (d M : Nat) (q ∈ pellPairs(d, M)) : pellCodes(M) :=75 ((pellDistance(d, q), chooseNat(isZero(sub(mul(d, mul(snd(q), snd(q))),76 mul(fst(q), fst(q)))), 1, 0)),77 (mod(fst(q), pellDistance(d, q)), mod(snd(q), pellDistance(d, q))))78requires nonsquare : ¬IsSquare(d)79proof80 have positive := pellDistance.positive with nonsquare, member81 have inProduct := (pellPairs.membership.forward with member).left82 obtain ⟨_, x, xIn, y, yIn, shape⟩ :=83 product.membership.forward with inProduct84 rw shape at member85 rw [shape, pellDistance.value] at positive86 obtain ⟨_, distLess⟩ :=87 (pellPairs.mem_iff at d, M, x, y).forward with member88 rw [shape, pellDistance.value, fst.pair, snd.pair]89 have MNat : Nat(M) by typecheck90 have kNat := natDist.natural at x * x, d * (y * y)91 have kNonzero : natDist(x * x, d * (y * y)) ≠ 0 by arith92 have distIn := membership.of_Less with kNat, MNat, distLess93 have signIn := chooseNat.member_two94 with (isZero.boolean at d * (y * y) - x * x)95 have xModNat := mod.natural at natDist(x * x, d * (y * y)), x96 have yModNat := mod.natural at natDist(x * x, d * (y * y)), y97 have xModLess := mod.less at x with kNonzero98 have yModLess := mod.less at y with kNonzero99 have xModBelow : x % natDist(x * x, d * (y * y)) < M by arith100 have yModBelow : y % natDist(x * x, d * (y * y)) < M by arith101 have xModIn := membership.of_Less with xModNat, MNat, xModBelow102 have yModIn := membership.of_Less with yModNat, MNat, yModBelow103 unfold pellCodes104 exact product.pair with (product.pair with distIn, signIn),105 (product.pair with xModIn, yModIn)106107-- The sign bit is 1 exactly when d y² ≤ x², and 0 when x² < d y².108theorem pellSign.cases (d x y : Nat)109 shows (chooseNat(isZero(d * (y * y) - x * x), 1, 0) = 1 ∧110 d * (y * y) ≤ x * x) ∨111 (chooseNat(isZero(d * (y * y) - x * x), 1, 0) = 0 ∧ x * x < d * (y * y))112proof113 by_cases below : d * (y * y) ≤ x * x114 . left115 have zero := sub.eq_zero_of_le with below116 rw zero117 simp [isZero, chooseNat]118 . right119 have above := Less.of_not_LessEqual with below120 have positive := sub.positive_of_Less with above121 have shape := natural.zero_or_successor at d * (y * y) - x * x122 cases shape with zero | successor123 . rw zero at positive124 exfalso125 arith126 . obtain ⟨m, mNat, form⟩ := successor127 rw form128 simp [isZero, chooseNat]129130-- Two pairs with the same code have the same signed value x² − d y² = ±k131-- and congruent coordinates modulo k.132133theorem pellCode.same (d M a b c e : Nat)134 requires nonsquare : ¬IsSquare(d)135 requires first : (a, b) ∈ pellPairs(d, M)136 requires second : (c, e) ∈ pellPairs(d, M)137 requires same : pellCode(d, M)((a, b)) = pellCode(d, M)((c, e))138 shows ∃ k, Nat(k) ∧ 0 < k ∧139 ((a * a = d * (b * b) + k ∧ c * c = d * (e * e) + k) ∨140 (d * (b * b) = a * a + k ∧ d * (e * e) = c * c + k)) ∧141 a % k = c % k ∧ b % k = e % k142proof143 have firstValue := pellCode.value at d, M, (a, b) with nonsquare, first144 have secondValue := pellCode.value at d, M, (c, e) with nonsquare, second145 rw [firstValue, secondValue] at same146 rw [pellDistance.value, pellDistance.value] at same147 rw [fst.pair, fst.pair, snd.pair, snd.pair] at same148 obtain ⟨outer, inner⟩ := orderedPair.injective with same149 obtain ⟨sameDist, sameSign⟩ := orderedPair.injective with outer150 obtain ⟨sameX, sameY⟩ := orderedPair.injective with inner151 have positive := pellDistance.positive with nonsquare, first152 rw pellDistance.value at positive153 rw ← sameDist at sameX154 rw ← sameDist at sameY155 witness natDist(a * a, d * (b * b))156 refine ⟨positive, ?_, sameX, sameY⟩157 have signA := pellSign.cases at d, a, b158 have signC := pellSign.cases at d, c, e159 cases signA with aPlus | aMinus160 . cases signC with cPlus | cMinus161 . -- Both differences x² − d y² are nonnegative.162 left163 have valueA := natDist.of_ge with aPlus.right164 have valueC := natDist.of_ge with cPlus.right165 have cancelA := sub.add_cancel_left with aPlus.right166 have cancelC := sub.add_cancel_left with cPlus.right167 rw ← valueA at cancelA168 rw ← valueC at cancelC169 rw ← sameDist at cancelC170 constructor171 . arith172 . arith173 . exfalso174 rw [aPlus.left, cMinus.left] at sameSign175 arith176 . cases signC with cPlus | cMinus177 . exfalso178 rw [aMinus.left, cPlus.left] at sameSign179 arith180 . -- Both differences are negative.181 right182 obtain ⟨_, aLess⟩ := aMinus183 obtain ⟨_, cLess⟩ := cMinus184 have aBelow : a * a ≤ d * (b * b) by arith185 have cBelow : c * c ≤ d * (e * e) by arith186 have valueA := natDist.of_le with aBelow187 have valueC := natDist.of_le with cBelow188 have cancelA := sub.add_cancel_left with aBelow189 have cancelC := sub.add_cancel_left with cBelow190 rw ← valueA at cancelA191 rw ← valueC at cancelC192 rw ← sameDist at cancelC193 constructor194 . arith195 . arith196197-- Composition. Brahmagupta's identity in ℤ: when C ≡ A and E ≡ B modulo198-- K with A² − D B² = C² − D E² = ±K, the composite (A C − D B E, A E − B C)199-- is K times a solution of X² − D Y² = 1, whose absolute values solve the200-- natural equation; Y ≠ 0 when the cross term A E − B C is nonzero.201202theorem pell.integer_compose (A B C E K P Q R S sign U V : Integer, d : Nat)203 requires UDef : U = A * (Q - P) - integerOfNat(d) * (B * (S - R)) + sign204 requires VDef : V = A * (S - R) - B * (Q - P)205 requires cDiff : C = A + K * (Q - P)206 requires eDiff : E = B + K * (S - R)207 requires first : A * A - integerOfNat(d) * (B * B) = sign * K208 requires second : C * C - integerOfNat(d) * (E * E) = sign * K209 requires unit : sign * sign = 1210 requires nonzero : K ≠ 0211 requires cross : A * E - B * C ≠ 0212 shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < y ∧ x * x = d * (y * y) + 1213proof214 have brahmagupta : (A * C - integerOfNat(d) * (B * E)) *215 (A * C - integerOfNat(d) * (B * E)) -216 integerOfNat(d) * ((A * E - B * C) * (A * E - B * C)) =217 (A * A - integerOfNat(d) * (B * B)) *218 (C * C - integerOfNat(d) * (E * E))219 ring220 have product : (A * A - integerOfNat(d) * (B * B)) *221 (C * C - integerOfNat(d) * (E * E)) = K * K222 rw [first, second]223 linear_combination K * K * unit224 have uForm : A * C - integerOfNat(d) * (B * E) = K * U225 linear_combination A * cDiff - integerOfNat(d) * B * eDiff + first -226 K * UDef227 have vForm : A * E - B * C = K * V228 linear_combination A * eDiff - B * cDiff - K * VDef229 rw [uForm, vForm, product] at brahmagupta230 have squareNonzero := integerMulValue.nonzero with nonzero, nonzero231 have scaled : (K * K) * (U * U - integerOfNat(d) * (V * V)) =232 (K * K) * integerOne233 linear_combination brahmagupta234 have pell := integerMulValue.left_cancel with squareNonzero, scaled235 -- V ≠ 0, since K V is the cross term.236 have VNonzero : V ≠ 0237 intro VZero238 rw VZero at vForm239 apply cross240 linear_combination vForm241 have uSquare := Integer.square_natAbs at U242 have vSquare := Integer.square_natAbs at V243 symm at uSquare244 symm at vSquare245 witness integerNatAbs(U)246 witness integerNatAbs(V)247 constructor248 . have notZero : integerNatAbs(V) ≠ 0249 intro zero250 have VZero := (integerNatAbs.zero_iff at V).forward with zero251 exact VNonzero with VZero252 exact Less.positive_of_ne_zero with notZero253 . have castSolution : integerOfNat(integerNatAbs(U) * integerNatAbs(U)) =254 integerOfNat(d * (integerNatAbs(V) * integerNatAbs(V)) + 1)255 rw [integerOfNat.add, integerOfNat.one, uSquare, integerOfNat.mul,256 vSquare]257 linear_combination pell258 exact integerOfNat.injective with castSolution259260-- A vanishing cross term a e = b c forces the two pairs to coincide: the261-- Pell equations give k (e² − b²) = 0, so e = b, and then a = c.262theorem pell.equal_of_cross (d a b c e k : Nat)263 requires positive : 0 < k264 requires same : (a * a = d * (b * b) + k ∧ c * c = d * (e * e) + k) ∨265 (d * (b * b) = a * a + k ∧ d * (e * e) = c * c + k)266 requires cross : a * e = b * c267 shows (a, b) = (c, e)268proof269 have kNonzero : k ≠ 0 by arith270 have kIntNonzero : integerOfNat(k) ≠ 0271 intro zero272 have kZero := (integerOfNat.eq_zero_iff at k).forward with zero273 exact kNonzero with kZero274 have castCross : integerOfNat(a * e) = integerOfNat(b * c)275 rw cross276 simp [integerOfNat.mul] at castCross277 -- k (e² − b²) = 0 in ℤ, in either sign case.278 have scaled : integerOfNat(k) * (integerOfNat(e) * integerOfNat(e) -279 integerOfNat(b) * integerOfNat(b)) = integerOfNat(k) * 0280 cases same with plus | minus281 . have castA : integerOfNat(a * a) = integerOfNat(d * (b * b) + k)282 rw plus.left283 have castC : integerOfNat(c * c) = integerOfNat(d * (e * e) + k)284 rw plus.right285 simp [integerOfNat.mul, integerOfNat.add] at castA286 simp [integerOfNat.mul, integerOfNat.add] at castC287 linear_combination (integerOfNat(b) * integerOfNat(b)) * castC -288 (integerOfNat(e) * integerOfNat(e)) * castA +289 (integerOfNat(a) * integerOfNat(e) +290 integerOfNat(b) * integerOfNat(c)) * castCross291 . have castA : integerOfNat(d * (b * b)) = integerOfNat(a * a + k)292 rw minus.left293 have castC : integerOfNat(d * (e * e)) = integerOfNat(c * c + k)294 rw minus.right295 simp [integerOfNat.mul, integerOfNat.add] at castA296 simp [integerOfNat.mul, integerOfNat.add] at castC297 linear_combination (integerOfNat(b) * integerOfNat(b)) * castC -298 (integerOfNat(e) * integerOfNat(e)) * castA -299 (integerOfNat(a) * integerOfNat(e) +300 integerOfNat(b) * integerOfNat(c)) * castCross301 have difference := integerMulValue.left_cancel with kIntNonzero, scaled302 have squares : integerOfNat(e) * integerOfNat(e) =303 integerOfNat(b) * integerOfNat(b)304 linear_combination difference305 have eEqual := integerOfNat.eq_of_square_eq with squares306 rw eEqual at cross307 rw eEqual at same308 rw eEqual309 -- a b = b c: either b = 0, or cancel b.310 have bShape := natural.zero_or_successor at b311 cases bShape with bZero | bSucc312 . rw bZero at same313 rw bZero314 cases same with plus | minus315 . have aSquare : a * a = c * c by arith316 have castSquare : integerOfNat(a * a) = integerOfNat(c * c)317 rw aSquare318 simp [integerOfNat.mul] at castSquare319 have aEqual := integerOfNat.eq_of_square_eq with castSquare320 rw aEqual321 . exfalso322 obtain ⟨aEquation, _⟩ := minus323 have zero : d * (0 * 0) = 0 by ring324 rw zero at aEquation325 arith326 . obtain ⟨m, mNat, form⟩ := bSucc327 have bNonzero : b ≠ 0328 rw form329 arith330 have swapped : b * a = b * c331 linear_combination cross332 have aEqual := mul.left_cancel_positive with bNonzero, swapped333 rw aEqual334335-- Composition: k X = a c − d b e and k Y = a e − c b are integers by the336-- congruences, X² − d Y² = 1 by linear_combination from the two337-- equations, and Y = 0 would force (a, b) = (c, e).338theorem pell.compose (d a b c e k : Nat)339 requires above : 1 < d340 requires positive : 0 < k341 requires same : (a * a = d * (b * b) + k ∧ c * c = d * (e * e) + k) ∨342 (d * (b * b) = a * a + k ∧ d * (e * e) = c * c + k)343 requires congruentX : a % k = c % k344 requires congruentY : b % k = e % k345 requires distinct : (a, b) ≠ (c, e)346 shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < y ∧ x * x = d * (y * y) + 1347proof348 have kNonzero : k ≠ 0 by arith349 have kIntNonzero : integerOfNat(k) ≠ 0350 intro zero351 have kZero := (integerOfNat.eq_zero_iff at k).forward with zero352 exact kNonzero with kZero353 -- The quotients of the four coordinates by k, with shared remainders.354 obtain ⟨pa, paNat, aShape⟩ := (mod.division at k, a with kNonzero).left355 obtain ⟨pc, pcNat, cShape⟩ := (mod.division at k, c with kNonzero).left356 obtain ⟨pb, pbNat, bShape⟩ := (mod.division at k, b with kNonzero).left357 obtain ⟨pe, peNat, eShape⟩ := (mod.division at k, e with kNonzero).left358 rw ← congruentX at cShape359 rw ← congruentY at eShape360 have castA : integerOfNat(a) = integerOfNat(k * pa + a % k)361 rw ← aShape362 have castC : integerOfNat(c) = integerOfNat(k * pc + a % k)363 rw ← cShape364 have castB : integerOfNat(b) = integerOfNat(k * pb + b % k)365 rw ← bShape366 have castE : integerOfNat(e) = integerOfNat(k * pe + b % k)367 rw ← eShape368 simp [integerOfNat.mul, integerOfNat.add] at castA369 simp [integerOfNat.mul, integerOfNat.add] at castC370 simp [integerOfNat.mul, integerOfNat.add] at castB371 simp [integerOfNat.mul, integerOfNat.add] at castE372 have cDiff : integerOfNat(c) = integerOfNat(a) +373 integerOfNat(k) * (integerOfNat(pc) - integerOfNat(pa))374 linear_combination castC - castA375 have eDiff : integerOfNat(e) = integerOfNat(b) +376 integerOfNat(k) * (integerOfNat(pe) - integerOfNat(pb))377 linear_combination castE - castB378 -- The signed Pell equations in ℤ.379 have signed : ∃ sign ∈ ℤ, sign * sign = 1 ∧380 integerOfNat(a) * integerOfNat(a) -381 integerOfNat(d) * (integerOfNat(b) * integerOfNat(b)) =382 sign * integerOfNat(k) ∧383 integerOfNat(c) * integerOfNat(c) -384 integerOfNat(d) * (integerOfNat(e) * integerOfNat(e)) =385 sign * integerOfNat(k)386 cases same with plus | minus387 . have castFirst : integerOfNat(a * a) = integerOfNat(d * (b * b) + k)388 rw plus.left389 have castSecond : integerOfNat(c * c) = integerOfNat(d * (e * e) + k)390 rw plus.right391 simp [integerOfNat.mul, integerOfNat.add] at castFirst392 simp [integerOfNat.mul, integerOfNat.add] at castSecond393 have oneIn : integerOne ∈ ℤ by typecheck394 witness integerOne395 refine ⟨oneIn, ?_, ?_, ?_⟩396 . ring397 . linear_combination castFirst398 . linear_combination castSecond399 . have castFirst : integerOfNat(d * (b * b)) = integerOfNat(a * a + k)400 rw minus.left401 have castSecond : integerOfNat(d * (e * e)) = integerOfNat(c * c + k)402 rw minus.right403 simp [integerOfNat.mul, integerOfNat.add] at castFirst404 simp [integerOfNat.mul, integerOfNat.add] at castSecond405 have negOneIn : -integerOne ∈ ℤ by typecheck406 witness -integerOne407 refine ⟨negOneIn, ?_, ?_, ?_⟩408 . ring409 . linear_combination -castFirst410 . linear_combination -castSecond411 obtain ⟨sign, signIn, unit, first, second⟩ := signed412 -- The cross term a e − b c is nonzero, since the pairs differ.413 have crossNonzero : integerOfNat(a) * integerOfNat(e) -414 integerOfNat(b) * integerOfNat(c) ≠ 0415 intro zero416 have crossNat : integerOfNat(a * e) = integerOfNat(b * c)417 rw [integerOfNat.mul, integerOfNat.mul]418 linear_combination zero419 have crossEqual := integerOfNat.injective with crossNat420 have equal := pell.equal_of_cross with positive, same, crossEqual421 exact distinct with equal422 -- The composite coordinates.423 have namedX : ∃ X, Integer(X) ∧ X =424 integerOfNat(a) * (integerOfNat(pc) - integerOfNat(pa)) -425 integerOfNat(d) *426 (integerOfNat(b) * (integerOfNat(pe) - integerOfNat(pb))) + sign427 witness integerOfNat(a) * (integerOfNat(pc) - integerOfNat(pa)) -428 integerOfNat(d) *429 (integerOfNat(b) * (integerOfNat(pe) - integerOfNat(pb))) + sign430 rfl431 have namedY : ∃ Y, Integer(Y) ∧ Y =432 integerOfNat(a) * (integerOfNat(pe) - integerOfNat(pb)) -433 integerOfNat(b) * (integerOfNat(pc) - integerOfNat(pa))434 witness integerOfNat(a) * (integerOfNat(pe) - integerOfNat(pb)) -435 integerOfNat(b) * (integerOfNat(pc) - integerOfNat(pa))436 rfl437 obtain ⟨X, XInteger, XDef⟩ := namedX438 obtain ⟨Y, YInteger, YDef⟩ := namedY439 exact pell.integer_compose with XDef, YDef, cDiff, eDiff, first, second,440 unit, kIntNonzero, crossNonzero441442-- The theorem: more pairs than codes, pellPairs.many with n = 2 (3d)⁴ + 1443-- and pigeonhole.collision through pellCode restricted to that set, and444-- the composition of the two pairs that share a code.445446theorem pellCode.collision (d : Nat)447 requires nonsquare : ¬IsSquare(d)448 shows ∃ q ∈ pellPairs(d, 3 * d), ∃ r ∈ pellPairs(d, 3 * d),449 q ≠ r ∧ pellCode(d, 3 * d)(q) = pellCode(d, 3 * d)(r)450proof451 have finiteCodes := pellCodes.finite at 3 * d452 have count := pellCodes.cardinality at 3 * d453 obtain ⟨S, subset, counted⟩ :=454 pellPairs.many at d, ((3 * d) * 2) * ((3 * d) * (3 * d)) + 1455 with nonsquare456 have sizeS := cardinality.eq_of_has with counted457 have function := pellCode.function at d, 3 * d with nonsquare458 have restricted := restrict.domain_function with function, subset459 have more : cardinality(pellCodes(3 * d)) < cardinality(S)460 rw [count, sizeS]461 arith462 obtain ⟨q, qIn, r, rIn, different, sameValue⟩ :=463 pigeonhole.collision with restricted, finiteCodes, more464 have qValue := restrict.domain_apply with function, subset, qIn465 have rValue := restrict.domain_apply with function, subset, rIn466 rw [qValue, rValue] at sameValue467 have qPairs := subset with q, qIn468 have rPairs := subset with r, rIn469 witness q470 refine ⟨qPairs, ?_⟩471 witness r472 exact ⟨rPairs, different, sameValue⟩473474theorem pell.exists_nontrivial (d : Nat)475 requires nonsquare : ¬IsSquare(d)476 shows ∃ x, Nat(x) ∧ ∃ y, Nat(y) ∧ 0 < y ∧ x * x = d * (y * y) + 1477proof478 obtain ⟨q, qIn, r, rIn, different, sameCode⟩ :=479 pellCode.collision with nonsquare480 have above := IsSquare.above_one_of_not with nonsquare481 have qProduct := (pellPairs.membership.forward with qIn).left482 have rProduct := (pellPairs.membership.forward with rIn).left483 obtain ⟨_, a, aIn, b, bIn, qShape⟩ :=484 product.membership.forward with qProduct485 obtain ⟨_, c, cIn, e, eIn, rShape⟩ :=486 product.membership.forward with rProduct487 rw qShape at qIn488 rw rShape at rIn489 rw [qShape, rShape] at different490 rw [qShape, rShape] at sameCode491 obtain ⟨k, kNat, positive, signs, congruentX, congruentY⟩ :=492 pellCode.same with nonsquare, qIn, rIn, sameCode493 exact pell.compose with above, positive, signs, congruentX, congruentY,494 different495