Grace/Cumulus/NumberTheory/FourSquaresDescent.g17

Open in Workbench Download source

Imports

Grace.Cumulus.NumberTheory.FourSquareIdentity · Grace.Cumulus.NumberTheory.FourSquaresModPrime · Grace.Cumulus.NumberTheory.IntegerRemainder

Declarations

Source

1-- Fermat's descent for four squares: a multiple m p of a prime with2-- 1 < m < p that is a sum of four squares yields a smaller positive3-- multiple that is, so every prime is a sum of four squares.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.FourSquaresDescent10import Grace.Cumulus.NumberTheory.FourSquareIdentity11import Grace.Cumulus.NumberTheory.FourSquaresModPrime12import Grace.Cumulus.NumberTheory.IntegerRemainder1314-- Euler's identity modulo m1516-- A sum of four integer squares is the embedded sum of the squares of the17-- absolute values.18theorem fourSquares.sum_ofNat (e f g k : Integer)19  shows e * e + f * f + g * g + k * k = integerOfNat(20    integerNatAbs(e) * integerNatAbs(e) + integerNatAbs(f) * integerNatAbs(f) +21    integerNatAbs(g) * integerNatAbs(g) + integerNatAbs(k) * integerNatAbs(k))22proof23  have eSquare := Integer.square_natAbs at e24  have fSquare := Integer.square_natAbs at f25  have gSquare := Integer.square_natAbs at g26  have kSquare := Integer.square_natAbs at k27  rw [eSquare, fSquare, gSquare, kSquare]28  simp [integerOfNat.add]2930-- When a, b, c, d are congruent to e, f, g, k modulo m and m divides31-- a² + b² + c² + d², m divides each cross term of Euler's identity: the32-- first is congruent to a² + b² + c² + d², the others to 0.33theorem fourSquares.cross_terms (m : Nat) (a b c d e f g k : Integer)34  requires sum : IntegerDivides(integerOfNat(m), a * a + b * b + c * c + d * d)35  requires congruentA : IntegerCongruent(integerOfNat(m), a, e)36  requires congruentB : IntegerCongruent(integerOfNat(m), b, f)37  requires congruentC : IntegerCongruent(integerOfNat(m), c, g)38  requires congruentD : IntegerCongruent(integerOfNat(m), d, k)39  shows IntegerDivides(integerOfNat(m), a * e + b * f + c * g + d * k) ∧40    IntegerDivides(integerOfNat(m), a * f - b * e + c * k - d * g) ∧41    IntegerDivides(integerOfNat(m), a * g - b * k - c * e + d * f) ∧42    IntegerDivides(integerOfNat(m), a * k + b * g - c * f - d * e)43proof44  obtain ⟨q, qInt, hq⟩ := sum45  obtain ⟨qa, qaInt, ha⟩ := congruentA46  obtain ⟨qb, qbInt, hb⟩ := congruentB47  obtain ⟨qc, qcInt, hc⟩ := congruentC48  obtain ⟨qd, qdInt, hd⟩ := congruentD49  refine ⟨?_, ?_, ?_, ?_⟩50  . apply IntegerDivides.intro at integerOfNat(m),51      a * e + b * f + c * g + d * k, q - a * qa - b * qb - c * qc - d * qd52    linear_combination hq - a * ha - b * hb - c * hc - d * hd53  . apply IntegerDivides.intro at integerOfNat(m),54      a * f - b * e + c * k - d * g, b * qa - a * qb + d * qc - c * qd55    linear_combination b * ha - a * hb + d * hc - c * hd56  . apply IntegerDivides.intro at integerOfNat(m),57      a * g - b * k - c * e + d * f, c * qa - a * qc + b * qd - d * qb58    linear_combination c * ha - a * hc + b * hd - d * hb59  . apply IntegerDivides.intro at integerOfNat(m),60      a * k + b * g - c * f - d * e, d * qa - a * qd + c * qb - b * qc61    linear_combination d * ha - a * hd + c * hb - b * hc6263-- Dividing Euler's identity by m² when m divides every cross term.64theorem fourSquares.quotient (m : Nat) (a b c d e f g k : Integer)65  requires sum : IntegerDivides(integerOfNat(m), a * a + b * b + c * c + d * d)66  requires congruentA : IntegerCongruent(integerOfNat(m), a, e)67  requires congruentB : IntegerCongruent(integerOfNat(m), b, f)68  requires congruentC : IntegerCongruent(integerOfNat(m), c, g)69  requires congruentD : IntegerCongruent(integerOfNat(m), d, k)70  shows ∃ z ∈ ℤ, IntegerSumOfFourSquares(z) ∧71    (a * a + b * b + c * c + d * d) * (e * e + f * f + g * g + k * k) =72      integerOfNat(m * m) * z73proof74  have terms := fourSquares.cross_terms at m, a, b, c, d, e, f, g, k with *75  obtain ⟨⟨w, wInt, hw⟩, ⟨x, xInt, hx⟩, ⟨y, yInt, hy⟩, ⟨z, zInt, hz⟩⟩ :=76    terms77  have squares := IntegerSumOfFourSquares.intro at w, x, y, z78  have member : w * w + x * x + y * y + z * z ∈ ℤ by typecheck79  refine ⟨w * w + x * x + y * y + z * z, member, squares, ?_⟩80  rw euler.four_square_identity81  rw [← hw, ← hx, ← hy, ← hz, integerOfNat.mul]82  ring8384-- The least absolute residues: their squares sum to at most m²8586-- Four naturals each at most s/4 sum to at most s.87theorem fourSquares.sum_bound (s x y z w : Nat)88  requires boundX : x * 4 ≤ s89  requires boundY : y * 4 ≤ s90  requires boundZ : z * 4 ≤ s91  requires boundW : w * 4 ≤ s92  shows x + y + z + w ≤ s93proof94  have first : (x + y) * 4 ≤ s + s by arith95  have second : (z + w) * 4 ≤ s + s by arith96  have scaled : (x + y + z + w) * 4 ≤ s * 4 by arith97  have fourNonzero : 4 ≠ 098    intro zero99    arith100  exact LessEqual.mul_right_cancel_positive at x + y + z + w, s, 4101    with fourNonzero, scaled102103-- Four residues of absolute value at most m/2 have squares summing to at104-- most m².105theorem fourSquares.residue_bound (m : Nat) (e f g k : Integer)106  requires boundE : 2 * integerNatAbs(e) ≤ m107  requires boundF : 2 * integerNatAbs(f) ≤ m108  requires boundG : 2 * integerNatAbs(g) ≤ m109  requires boundK : 2 * integerNatAbs(k) ≤ m110  shows integerNatAbs(e * e + f * f + g * g + k * k) ≤ m * m111proof112  rw [fourSquares.sum_ofNat, integerNatAbs.ofNat]113  have squareE := integerNatAbs.square_bound at e, m with boundE114  have squareF := integerNatAbs.square_bound at f, m with boundF115  have squareG := integerNatAbs.square_bound at g, m with boundG116  have squareK := integerNatAbs.square_bound at k, m with boundK117  rw integerNatAbs.mul at squareE, squareF, squareG, squareK118  exact fourSquares.sum_bound with squareE, squareF, squareG, squareK119120-- The extreme cases: the squares sum to 0 or to m²121122-- In an extreme case, residues bounded by m/2 whose squares sum to 0 or123-- m² each have square 0 or exactly m²/4.124theorem fourSquares.extreme_squares (m : Nat) (e f g k : Integer)125  requires boundE : 2 * integerNatAbs(e) ≤ m126  requires boundF : 2 * integerNatAbs(f) ≤ m127  requires boundG : 2 * integerNatAbs(g) ≤ m128  requires boundK : 2 * integerNatAbs(k) ≤ m129  requires extreme : e * e + f * f + g * g + k * k = 0 ∨130    e * e + f * f + g * g + k * k = integerOfNat(m * m)131  shows (integerNatAbs(e) * integerNatAbs(e) = 0 ∨132      integerNatAbs(e) * integerNatAbs(e) * 4 = m * m) ∧133    (integerNatAbs(f) * integerNatAbs(f) = 0 ∨134      integerNatAbs(f) * integerNatAbs(f) * 4 = m * m) ∧135    (integerNatAbs(g) * integerNatAbs(g) = 0 ∨136      integerNatAbs(g) * integerNatAbs(g) * 4 = m * m) ∧137    (integerNatAbs(k) * integerNatAbs(k) = 0 ∨138      integerNatAbs(k) * integerNatAbs(k) * 4 = m * m)139proof140  have squareE := integerNatAbs.square_bound at e, m with boundE141  have squareF := integerNatAbs.square_bound at f, m with boundF142  have squareG := integerNatAbs.square_bound at g, m with boundG143  have squareK := integerNatAbs.square_bound at k, m with boundK144  rw integerNatAbs.mul at squareE, squareF, squareG, squareK145  rw fourSquares.sum_ofNat at extreme146  cases extreme with zero | full147  . have total := integerOfNat.eq_zero_iff.forward with zero148    refine ⟨?_, ?_, ?_, ?_⟩149    . left; arith150    . left; arith151    . left; arith152    . left; arith153  . have total := integerOfNat.injective with full154    refine ⟨?_, ?_, ?_, ?_⟩155    . right; arith156    . right; arith157    . right; arith158    . right; arith159160-- A residue whose square is 0 or m²/4 is 0 or ±m/2, so m divides twice it.161theorem fourSquares.residue_double (m : Nat) (e : Integer)162  requires extreme : integerNatAbs(e) * integerNatAbs(e) = 0 ∨163    integerNatAbs(e) * integerNatAbs(e) * 4 = m * m164  shows IntegerDivides(integerOfNat(m), e + e)165proof166  obtain ⟨n, nNat, sign⟩ := Integer.signed_natural at e167  have same := integerNatAbs.eq_of_sign at e, n with sign168  have eSquare := Integer.square_natAbs at e169  rw same at extreme, eSquare170  cases extreme with zero | half171  . rw [zero, integerOfNat.zero] at eSquare172    have eZero := integerMulValue.square_eq_zero at e with eSquare173    rw eZero174    apply IntegerDivides.intro at integerOfNat(m), integerZero + integerZero,175      integerZero176    ring177  . have expanded : n * n * 4 = n * n + n * n + n * n + n * n by ring178    rw expanded at half179    have lifted : integerOfNat(n * n + n * n + n * n + n * n) =180        integerOfNat(m * m)181      rw half182    rw integerOfNat.mul at lifted183    simp [integerOfNat.add] at lifted184    have product : (e + e - integerOfNat(m)) * (e + e + integerOfNat(m)) = 0185      linear_combination eSquare + eSquare + eSquare + eSquare + lifted186    have split := integerMulValue.eq_zero with product187    cases split with plus | minus188    . apply IntegerDivides.intro at integerOfNat(m), e + e, integerOne189      linear_combination -plus190    . apply IntegerDivides.intro at integerOfNat(m), e + e, -integerOne191      linear_combination -minus192193-- The extreme cases: residues all zero, or all of absolute value exactly194-- m/2; either way m² divides the original sum of squares.195theorem fourSquares.extreme (m : Nat) (a b c d e f g k : Integer)196  requires boundE : 2 * integerNatAbs(e) ≤ m197  requires boundF : 2 * integerNatAbs(f) ≤ m198  requires boundG : 2 * integerNatAbs(g) ≤ m199  requires boundK : 2 * integerNatAbs(k) ≤ m200  requires congruentA : IntegerCongruent(integerOfNat(m), a, e)201  requires congruentB : IntegerCongruent(integerOfNat(m), b, f)202  requires congruentC : IntegerCongruent(integerOfNat(m), c, g)203  requires congruentD : IntegerCongruent(integerOfNat(m), d, k)204  requires extreme : e * e + f * f + g * g + k * k = 0 ∨205    e * e + f * f + g * g + k * k = integerOfNat(m * m)206  shows IntegerDivides(integerOfNat(m * m), a * a + b * b + c * c + d * d)207proof208  -- Each residue has square 0 or m²/4, so m divides each doubled residue,209  -- and m² divides each piece of a² = (a − e)² + (a − e)(2e) + e².210  have parts := fourSquares.extreme_squares with *211  obtain ⟨partE, partF, partG, partK⟩ := parts212  have doubleE := fourSquares.residue_double at m, e with partE213  have doubleF := fourSquares.residue_double at m, f with partF214  have doubleG := fourSquares.residue_double at m, g with partG215  have doubleK := fourSquares.residue_double at m, k with partK216  obtain ⟨qa, qaInt, ha⟩ := congruentA217  obtain ⟨qb, qbInt, hb⟩ := congruentB218  obtain ⟨qc, qcInt, hc⟩ := congruentC219  obtain ⟨qd, qdInt, hd⟩ := congruentD220  obtain ⟨ue, ueInt, hue⟩ := doubleE221  obtain ⟨uf, ufInt, huf⟩ := doubleF222  obtain ⟨ug, ugInt, hug⟩ := doubleG223  obtain ⟨uk, ukInt, huk⟩ := doubleK224  have aShape : a = e + integerOfNat(m) * qa by linear_combination -ha225  have bShape : b = f + integerOfNat(m) * qb by linear_combination -hb226  have cShape : c = g + integerOfNat(m) * qc by linear_combination -hc227  have dShape : d = k + integerOfNat(m) * qd by linear_combination -hd228  rw [aShape, bShape, cShape, dShape, integerOfNat.mul]229  cases extreme with zero | full230  . refine ⟨qa * qa + qb * qb + qc * qc + qd * qd +231      (qa * ue + qb * uf + qc * ug + qd * uk), ?_, ?_⟩232    . typecheck233    . linear_combination integerOfNat(m) * qa * hue +234        integerOfNat(m) * qb * huf + integerOfNat(m) * qc * hug +235        integerOfNat(m) * qd * huk - zero236  . rw integerOfNat.mul at full237    refine ⟨qa * qa + qb * qb + qc * qc + qd * qd +238      (qa * ue + qb * uf + qc * ug + qd * uk) + 1, ?_, ?_⟩239    . typecheck240    . linear_combination integerOfNat(m) * qa * hue +241        integerOfNat(m) * qb * huf + integerOfNat(m) * qc * hug +242        integerOfNat(m) * qd * huk - full243244-- m² dividing m p with p prime forces m ∣ p, so m = 1 or m = p.245theorem fourSquares.no_extreme (p m : Nat)246  requires prime : Prime(p)247  requires aboveOne : 1 < m248  requires below : m < p249  shows ¬(m * m ∣ m * p)250proof251  intro divides252  have mNonzero : m ≠ 0 by arith253  have cancel := divides.mul_cancel_left at m, m, p with mNonzero254  have dividesP := cancel.forward with divides255  have one := Prime.divisor at p, m with *256  rw one at aboveOne257  arith258259-- The descent step260261-- Take least absolute residues e, …, k of a, …, d modulo m; their squares262-- sum to m r with 0 < r < m by the bound and the extreme cases, and263-- Euler's identity divided by m² writes r p as a sum of four squares.264theorem fourSquares.descent_step (p m : Nat)265  requires prime : Prime(p)266  requires aboveOne : 1 < m267  requires below : m < p268  requires sum : SumOfFourSquares(m * p)269  shows ∃ r, Nat(r) ∧ 0 < r ∧ r < m ∧ SumOfFourSquares(r * p)270proof271  obtain ⟨a, aNat, b, bNat, c, cNat, d, dNat, equation⟩ := sum272  have mPositive : 0 < m by arith273  have mNonzero : m ≠ 0 by arith274  -- The sum of squares in ℤ is m p, so m divides it and m² does not.275  have sumNat : integerOfNat(a) * integerOfNat(a) +276      integerOfNat(b) * integerOfNat(b) + integerOfNat(c) * integerOfNat(c) +277      integerOfNat(d) * integerOfNat(d) = integerOfNat(m * p)278    rw equation279    simp [integerOfNat.add, integerOfNat.mul]280  have divides : IntegerDivides(integerOfNat(m),281      integerOfNat(a) * integerOfNat(a) + integerOfNat(b) * integerOfNat(b) +282      integerOfNat(c) * integerOfNat(c) + integerOfNat(d) * integerOfNat(d))283    rw [sumNat, integerOfNat.mul]284    exact IntegerDivides.mul_left_self at integerOfNat(m), integerOfNat(p)285  have noSquare := fourSquares.no_extreme at p, m with *286  have squareDivides : ¬IntegerDivides(integerOfNat(m * m),287      integerOfNat(a) * integerOfNat(a) + integerOfNat(b) * integerOfNat(b) +288      integerOfNat(c) * integerOfNat(c) + integerOfNat(d) * integerOfNat(d))289    rw sumNat290    intro extreme291    have natural := IntegerDivides.ofNat_iff.forward with extreme292    exact noSquare with natural293  -- The least absolute residues.294  obtain ⟨e, eInt, congruentA, boundE⟩ :=295    IntegerCongruent.least_absolute at integerOfNat(a), m with mPositive296  obtain ⟨f, fInt, congruentB, boundF⟩ :=297    IntegerCongruent.least_absolute at integerOfNat(b), m with mPositive298  obtain ⟨g, gInt, congruentC, boundG⟩ :=299    IntegerCongruent.least_absolute at integerOfNat(c), m with mPositive300  obtain ⟨k, kInt, congruentD, boundK⟩ :=301    IntegerCongruent.least_absolute at integerOfNat(d), m with mPositive302  -- Their squares sum to a natural that m divides: m r, with r ≤ m.303  have residueSum := fourSquares.sum_ofNat at e, f, g, k304  have squaresA := IntegerCongruent.mul with congruentA, congruentA305  have squaresB := IntegerCongruent.mul with congruentB, congruentB306  have squaresC := IntegerCongruent.mul with congruentC, congruentC307  have squaresD := IntegerCongruent.mul with congruentD, congruentD308  have sumAB := IntegerCongruent.add with squaresA, squaresB309  have sumABC := IntegerCongruent.add with sumAB, squaresC310  have sumCongruent := IntegerCongruent.add with sumABC, squaresD311  have sumZero := IntegerCongruent.zero_right_iff.backward with divides312  have backward := IntegerCongruent.symmetric with sumCongruent313  have residueZero := IntegerCongruent.transitive with backward, sumZero314  have residueDivides :=315    IntegerCongruent.zero_right_iff.forward with residueZero316  rw residueSum at residueDivides317  have natural := IntegerDivides.ofNat_iff.forward with residueDivides318  obtain ⟨r, rNat, shape⟩ := divides.definition.forward with natural319  have bound := fourSquares.residue_bound at m, e, f, g, k with *320  rw [residueSum, integerNatAbs.ofNat, ← shape] at bound321  have rBound := LessEqual.mul_left_cancel_positive at r, m, m322    with mNonzero, bound323  rw ← shape at residueSum324  -- The extreme cases r = 0 and r = m would make m² divide m p.325  have notExtreme : ¬(e * e + f * f + g * g + k * k = 0 ∨326      e * e + f * f + g * g + k * k = integerOfNat(m * m))327    intro extreme328    have squareSum := fourSquares.extreme at m, integerOfNat(a),329      integerOfNat(b), integerOfNat(c), integerOfNat(d), e, f, g, k with *330    exact squareDivides with squareSum331  have rPositive : 0 < r332    by_contra zero333    have rZero : r = 0 by arith334    rw [rZero, mul.right_zero, integerOfNat.zero] at residueSum335    apply notExtreme336    left337    exact residueSum338  have rBelow : r < m339    by_contra notBelow340    have same : r = m by arith341    rw same at residueSum342    apply notExtreme343    right344    exact residueSum345  -- Euler's identity divided by m² writes r p as a sum of four squares.346  have quotient := fourSquares.quotient at m, integerOfNat(a), integerOfNat(b),347    integerOfNat(c), integerOfNat(d), e, f, g, k with *348  obtain ⟨z, zInt, zSquares, product⟩ := quotient349  rw [sumNat, residueSum] at product350  have key : integerOfNat(m * m) * integerOfNat(r * p) =351      integerOfNat(m * p) * integerOfNat(m * r)352    rw [← integerOfNat.mul, ← integerOfNat.mul]353    have same : m * m * (r * p) = m * p * (m * r) by ring354    rw same355  rw ← key at product356  have nonzero : integerOfNat(m * m) ≠ 0357    intro zero358    have squareZero := integerOfNat.eq_zero_iff.forward with zero359    have squareNonzero := mul.nonzero at m, m with mNonzero, mNonzero360    exact squareNonzero with squareZero361  have same := integerMulValue.left_cancel at integerOfNat(r * p), z,362    integerOfNat(m * m) with nonzero, product363  rw ← same at zSquares364  have result := SumOfFourSquares.of_integer at r * p with zSquares365  refine ⟨r, rNat, rPositive, rBelow, result⟩366367-- Every prime is a sum of four squares368369-- The positive multiples of p that are sums of four squares.370def fourSquareMultiples (p : Nat) : Set :=371  { m ∈ ℕ | 0 < m ∧ SumOfFourSquares(mul(m, p)) }372373-- A prime is 2 or of the form 2 h + 1.374theorem Prime.two_or_odd_half (p : Nat)375  requires prime : Prime(p)376  shows p = 2 ∨ ∃ h, Nat(h) ∧ p = 2 * h + 1377proof378  have parity := parity.exhaustive at p379  cases parity with even | odd380  . left381    have evenShape := Even.iff_two_mul at p382    obtain ⟨h, hNat, shape⟩ := evenShape.forward with even383    symm at shape384    have twoDivides := divides.intro at 2, p, h with shape385    have above := Prime.greater_than_one with prime386    have twoLe : 2 ≤ p by arith387    have split := LessEqual.cases at 2, p with twoLe388    cases split with less | same389    . exfalso390      have one := Prime.divisor at p, 2 with *391      arith392    . rw ← same393  . right394    have oddShape := Odd.iff_two_mul_add_one at p395    exact oddShape.forward with odd396397-- The least positive m with m p a sum of four squares is 1: the odd case398-- is seeded by Prime.multiple_sum_squares_one, and 2 = 1 + 1 + 0 + 0.399theorem lagrange.prime (p : Nat)400  requires prime : Prime(p)401  shows SumOfFourSquares(p)402proof403  have split := Prime.two_or_odd_half at p with prime404  cases split with two | odd405  . rw two406    exact SumOfFourSquares.two407  . obtain ⟨h, hNat, shape⟩ := odd408    obtain ⟨m, mNat, a, aNat, b, bNat, mPositive, mBelow, equation⟩ :=409      Prime.multiple_sum_squares_one at p, h with prime, shape410    -- m is a positive multiple of p that is a sum of four squares.411    have seedSum : SumOfFourSquares(mul(m, p))412      have same : m * p = a * a + b * b + 1 * 1 + 0 * 0413        rw equation414        ring415      rw same416      exact SumOfFourSquares.intro at a, b, 1, 0417    have mMember := nat.omega.forward with mNat418    have seed : m ∈ fourSquareMultiples(p) := ⟨mMember, mPositive, seedSum⟩419    have inhabited : Nonempty(fourSquareMultiples(p)) := ⟨m, seed⟩420    have naturals : fourSquareMultiples(p) ⊆ ℕ by simp421    have least := leastNatural.member at fourSquareMultiples(p) with *422    obtain ⟨leastMember, leastPositive, leastSum⟩ :=423      fourSquareMultiples.membership.forward with least424    have leastBound := leastNatural.least at fourSquareMultiples(p), m with *425    -- The least multiple is not above 1, since the descent would go lower.426    have notAbove : ¬(1 < leastNatural(fourSquareMultiples(p)))427      intro above428      have belowP : leastNatural(fourSquareMultiples(p)) < p by arith429      obtain ⟨r, rNat, rPositive, rBelow, rSum⟩ := fourSquares.descent_step430        at p, leastNatural(fourSquareMultiples(p)) with prime, above, belowP,431        leastSum432      have rMember := nat.omega.forward with rNat433      have rIn : r ∈ fourSquareMultiples(p) := ⟨rMember, rPositive, rSum⟩434      have rBound := leastNatural.least at fourSquareMultiples(p), r with *435      arith436    have one : leastNatural(fourSquareMultiples(p)) = 1 by arith437    rw [one, mul.left_unit] at leastSum438    exact leastSum439