Existing in Mathlib · Number theory
Fermat's Two-Square Classification
A natural number is a sum of two natural-number squares exactly when every prime factor leaving residue 3 modulo 4 occurs to an even exponent. Under Mathlib's valuation convention, zero is included.
- sum of two squares
- prime-factor classification
- parity of valuations
- quadratic residues
- multiplicative representations
Exact theorem
Exact Mathlib statement
theorem Nat.eq_sq_add_sq_iff {n : ℕ} : (∃ x y : ℕ, n = x ^ 2 + y ^ 2) ↔ ∀ q ∈ n.primeFactors, q % 4 = 3 → Even (padicValNat q n)The theorem at a glance
Fermat's Two-Square Classification — the exact prime-factor criterion
A visual guide to the theorem's hypotheses, structure, and conclusion; the exact statement gives the formal detail.
Fermat's Two-Square Classification — the exact prime-factor criterion

Detailed visual description
The classification connects a geometric-looking representation to arithmetic factor data. The source moves through the square-modulo-minus-one criterion and a square-times-squarefree decomposition before translating the condition into parity of prime exponents.
Statement structure
Statement and scope
Read the exact Mathlib declaration
This map summarizes the statement's structure; the exact Mathlib declaration remains authoritative.
Two squares correspond to paired 3 mod 4 prime factors

Detailed visual description
One side assembles a number from two perpendicular square contributions. The other groups its prime-power factors by residue modulo four, pairing every factor from the 3 mod 4 class while leaving the other classes unrestricted.
Why it matters
A mathematical landmark
The two-square theorem is a model example of local-to-global arithmetic: a concrete additive representation is governed exactly by parity conditions in the prime factorization.
ProofAtlas record
What has been checked
Mathlib is the source of the theorem; the local Lean replay and page review are separate.
Claim boundary
No new theorem is claimed
This target indexes Mathlib's classification of all natural numbers representable as a sum of two natural-number squares. It is not only the prime case: representability is equivalent to every prime factor congruent to 3 modulo 4 occurring with even padicValNat exponent. The convention includes n = 0.
- The selected endpoint classifies all natural numbers; it is not merely the prime 1 mod 4 theorem.
- It asserts existence of a representation, not uniqueness or a count of representations.
- Prime factors congruent to 1 or 2 modulo 4 are not subject to the even-exponent condition.
- The exact convention includes n = 0, for which both sides hold.
- ProofAtlas is indexing an existing Mathlib theorem, not claiming new mathematics.
Source and local evidence
Where the theorem comes from
- Existing declaration
Nat.eq_sq_add_sq_iffin mathlib- Relationship
- The checked artifact is the upstream declaration itself
- Local Lean evidence
artifact.library.mathlib.fermat-two-square-theorem.v001