Grace/Cumulus/NumberTheory/TwoSquares.g17
Imports
Grace.Cumulus.NumberTheory.Thue · Grace.Cumulus.NumberTheory.EulerCriterion · Grace.Cumulus.NumberTheory.PerfectSquares
Declarations
- Prime.one_mod_four_half theorem derivation
- fermat.square_sum_of_congruent theorem derivation
- fermat.square_sum_multiple theorem derivation
- fermat.two_squares theorem derivation
- Prime.sum_of_two_squares theorem derivation
- Prime.odd_of_not_two theorem derivation
- Prime.sum_of_two_squares_iff theorem derivation
Source
1-- Fermat's theorem on sums of two squares: a prime p ≡ 1 modulo 4 is a2-- sum of two squares, by the first supplement and Thue's lemma; an odd3-- prime is a sum of two squares exactly when it is 1 modulo 4.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.TwoSquares10import Grace.Cumulus.NumberTheory.Thue11import Grace.Cumulus.NumberTheory.EulerCriterion12import Grace.Cumulus.NumberTheory.PerfectSquares1314-- The square sum of Thue's pair1516-- p = 4j + 1 is 2h + 1 with h = 2j even.17theorem Prime.one_mod_four_half (p : Nat)18 requires prime : Prime(p)19 requires residue : p % 4 = 120 shows ∃ h, Nat(h) ∧ p = 2 * h + 1 ∧ Even(h)21proof22 have four : 4 ≠ 0 by arith23 obtain ⟨j, jNat, shape⟩ := mod.equation at p, 4 with four24 rw residue at shape25 have doubleNat : Nat(2 * j) by typecheck26 have even := Even.two_mul at j27 refine ⟨2 * j, doubleNat, ?_, even⟩28 rw shape29 ring3031-- y² ≡ a² x² and a² + 1 ≡ 0 give x² + y² ≡ (a² + 1) x² ≡ 0.32theorem fermat.square_sum_of_congruent (a p x y : Nat)33 requires root : (a * a + 1) % p = 034 requires squares : CongruentMod(y * y, a * x * (a * x), p)35 shows (x * x + y * y) % p = 036proof37 have rootCongruent : CongruentMod(a * a + 1, 0, p)38 unfold CongruentMod39 rw mod.zero_left40 exact root41 have scaled := CongruentMod.mul_right at a * a + 1, 0, x * x42 with rootCongruent43 have shape : (a * a + 1) * (x * x) = x * x + a * x * (a * x) by ring44 have zeroShape : 0 * (x * x) = 0 by ring45 rw [shape, zeroShape] at scaled46 have added := CongruentMod.add_left at y * y, a * x * (a * x), x * x47 with squares48 have total := CongruentMod.transitive with added, scaled49 unfold CongruentMod at total50 rw mod.zero_left at total51 exact total5253-- With a² + 1 ≡ 0, Thue's pair has x² + y² ≡ (1 + a²) x² ≡ 0 in either54-- sign case: y ≡ a x gives y² ≡ a² x² at once, and a x + y ≡ 0 gives55-- a x y + a² x² ≡ 0 ≡ a x y + y², so again y² ≡ a² x².56theorem fermat.square_sum_multiple (a p m x y : Nat)57 requires root : (a * a + 1) % p = 058 requires congruent : (a * x) % p = y % p ∨ (a * x + y) % p = 059 shows (x * x + y * y) % p = 060proof61 cases congruent with plus | minus62 . have first : CongruentMod(a * x, y, p)63 unfold CongruentMod64 exact plus65 have squares := CongruentMod.mul with first, first66 have symmetric := CongruentMod.symmetric with squares67 exact fermat.square_sum_of_congruent with root, symmetric68 . have zero : CongruentMod(a * x + y, 0, p)69 unfold CongruentMod70 rw mod.zero_left71 exact minus72 have byX := CongruentMod.mul_right at a * x + y, 0, a * x with zero73 have byY := CongruentMod.mul_right at a * x + y, 0, y with zero74 have xShape : (a * x + y) * (a * x) = a * x * y + a * x * (a * x) by ring75 have yShape : (a * x + y) * y = a * x * y + y * y by ring76 have zeroX : 0 * (a * x) = 0 by ring77 have zeroY : 0 * y = 0 by ring78 rw [xShape, zeroX] at byX79 rw [yShape, zeroY] at byY80 have reverse := CongruentMod.symmetric with byY81 have both := CongruentMod.transitive with byX, reverse82 have squares := CongruentMod.add_left_cancel with both83 have symmetric := CongruentMod.symmetric with squares84 exact fermat.square_sum_of_congruent with root, symmetric8586-- Fermat's theorem8788-- 0 < x² + y² ≤ 2p with x, y < squareBound(p), and x² + y² = 2p would89-- make x² = y² = p a square; so the multiple of p is p itself.90theorem fermat.two_squares (p : Nat)91 requires prime : Prime(p)92 requires residue : p % 4 = 193 shows ∃ a, Nat(a) ∧ ∃ b, Nat(b) ∧ a * a + b * b = p94proof95 obtain ⟨h, hNat, odd, even⟩ := Prime.one_mod_four_half with prime, residue96 obtain ⟨a, aNat, root⟩ := negOne.square with prime, odd, even97 have aboveOne := Prime.greater_than_one with prime98 have positive : 0 < p by arith99 have bound := squareBound.above at p100 have below := squareBound.le with aboveOne101 obtain ⟨x, xNat, y, yNat, xPositive, xLess, yLess, congruent⟩ :=102 thue.natural at a, p, squareBound(p) with positive, bound, below103 have multiple := fermat.square_sum_multiple at a, p, squareBound(p), x, y104 with root, congruent105 have xSquare := squareBound.least with xLess106 have ySquare := squareBound.least with yLess107 have squarePositive := Less.mul_positive with xPositive, xPositive108 have nonzero := Prime.nonzero with prime109 have characterization := mod.zero_iff at p, x * x + y * y with nonzero110 have divides := characterization.forward with multiple111 have definition := divides.definition at p, x * x + y * y112 obtain ⟨k, kNat, product⟩ := definition.forward with divides113 -- p k = x² + y² ≤ 2p, so k is at most 2.114 have kBelow : k < 3115 by_contra notBelow116 have threeLe : 3 ≤ k by arith117 have scaled := LessEqual.mul at p, p, 3, k with118 (LessEqual.reflexive at p), threeLe119 arith120 have alternatives := natural.below_three with kBelow121 rcases alternatives with zero | one | two122 . -- k = 0 contradicts 0 < x².123 exfalso124 rw zero at product125 have zeroShape : p * 0 = 0 by ring126 rw zeroShape at product127 arith128 . rw one at product129 have oneShape : p * 1 = p by ring130 rw oneShape at product131 refine ⟨x, xNat, y, yNat, ?_⟩132 symm133 exact product134 . -- k = 2 forces x² = p, but a prime is not a square.135 exfalso136 rw two at product137 have atLeast : p ≤ x * x by arith138 have same := LessEqual.antisymmetric with xSquare, atLeast139 have square := IsSquare.intro at x140 rw same at square141 have notSquare := Prime.not_square with prime142 exact notSquare with square143144theorem Prime.sum_of_two_squares (p : Nat)145 requires prime : Prime(p)146 requires residue : p % 4 = 1147 shows SumOfTwoSquares(p)148proof149 obtain ⟨a, aNat, b, bNat, sum⟩ :=150 fermat.two_squares at p with prime, residue151 rw ← sum152 exact SumOfTwoSquares.intro at a, b153154-- The converse for odd primes155156-- A prime other than 2 is odd: an even prime has the proper divisor 2.157theorem Prime.odd_of_not_two (p : Nat)158 requires prime : Prime(p)159 requires distinct : p ≠ 2160 shows Odd(p)161proof162 have parity := parity.exhaustive at p163 cases parity with even | odd164 . exfalso165 have characterization := divides.two_iff_even at p166 have divides := characterization.backward with even167 have nonzero := Prime.nonzero with prime168 have bound := divides.le at 2, p with divides, nonzero169 have alternatives := LessEqual.cases at 2, p with bound170 cases alternatives with strict | same171 . have twoNat : Nat(2) by typecheck172 have proper := membership.of_Less at 2, p with *, strict173 have one := Prime.divisor at p, 2 with prime, twoNat, proper, divides174 arith175 . apply distinct176 symm177 exact same178 . exact odd179180-- An odd prime is 1 or 3 modulo 4, and a sum of two squares is never 3.181theorem Prime.sum_of_two_squares_iff (p : Nat)182 requires prime : Prime(p)183 requires odd : p ≠ 2184 shows SumOfTwoSquares(p) ↔ p % 4 = 1185proof186 constructor187 . intro sum188 have notThree := SumOfTwoSquares.mod_four with sum189 have oddPrime := Prime.odd_of_not_two at p with prime, odd190 have residues := Odd.mod_four with oddPrime191 cases residues with one | three192 . exact one193 . exfalso194 exact notThree with three195 . intro residue196 exact Prime.sum_of_two_squares with prime, residue197