Grace/Cumulus/NumberTheory/FourSquares.g17
Imports
Grace.Cumulus.NumberTheory.FourSquaresDescent · Grace.Cumulus.NumberTheory.Factorization
Declarations
- SumOfFourSquares.listProduct theorem derivation
- SumOfFourSquares.all theorem derivation
- lagrange.four_squares theorem derivation
Source
1-- Lagrange's four-square theorem: every natural number is a sum of four2-- squares, by the prime case and the product identity along a prime3-- factorization.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.FourSquares10import Grace.Cumulus.NumberTheory.FourSquaresDescent11import Grace.Cumulus.NumberTheory.Factorization1213-- Products of primes1415theorem SumOfFourSquares.listProduct (xs : List(ℕ))16 requires primes : PrimeList(xs)17 shows SumOfFourSquares(∏ xs)18proof19 induction xs20 | List.nil =>21 intro primes22 rw listProduct.nil23 exact SumOfFourSquares.one24 | List.cons(x, rest) using ih =>25 intro primes26 have head := PrimeList.head at x, rest with primes27 have tail := PrimeList.tail at x, rest with primes28 have restSum := ih with tail29 have headSum := lagrange.prime at x with head30 rw listProduct.cons31 exact SumOfFourSquares.mul at x, ∏ rest with headSum, restSum3233-- Lagrange's theorem3435theorem SumOfFourSquares.all (n : Nat)36 shows SumOfFourSquares(n)37proof38 by_cases zero : n = 039 . rw zero40 exact SumOfFourSquares.zero41 . obtain ⟨xs, xsList, primes, product⟩ :=42 primeFactorization.exists at n with zero43 rw ← product44 exact SumOfFourSquares.listProduct at xs with primes4546theorem lagrange.four_squares (n : Nat)47 shows ∃ a, Nat(a) ∧ ∃ b, Nat(b) ∧ ∃ c, Nat(c) ∧ ∃ d, Nat(d) ∧48 a * a + b * b + c * c + d * d = n49proof50 obtain ⟨a, aNat, b, bNat, c, cNat, d, dNat, equation⟩ :=51 SumOfFourSquares.all at n52 symm at equation53 refine ⟨a, aNat, b, bNat, c, cNat, d, dNat, equation⟩54