Writing a positive integer as a sum of two squares is an elementary question with a precise general answer. Some integers have such a representation and others do not; checking a few small inputs does not explain the distinction. A criterion expressed through prime factorization instead decides the question for every positive integer. This project follows Section 5.7 of William Stein's Elementary Number Theory: Primes, Congruences, and Secrets, including the section's supporting statements and one subsequent exercise. The selected material connects divisibility, coprimality, algebraic identities, and rational approximation within a single classical topic. The source is the author-hosted January 2017 text, using its numbering rather than the numbering of earlier drafts.
A two-square representation of an integer consists of integers satisfying . Either coordinate may be zero or negative. A representation is primitive when the greatest common divisor of its coordinates is one; this restricts representations, not the definition of representability itself. For a positive integer and a prime , the prime exponent is the exponent of in the prime factorization of . The congruence means that division of by four leaves remainder three.
The approximation statement uses a real number , a positive integer , and a reduced fraction , where is an integer, is a positive integer, and their greatest common divisor is one. These conventions agree with Stein's section and its definition of primitive representations.
The supporting targets retain their complete source statements. Lemma 5.7.4 concerns every positive integer with a prime divisor :
Equation (5.7.1) is the integer identity
Lemma 5.7.5 states that, for every real and positive integer , some reduced fraction satisfies
The capstone, Theorem 5.7.1, is the complete equivalence
for every positive integer . Both implications are required. These four statements are located on printed pages 117–120 of the source PDF.
Exercise 5.11, on printed page 122, is an optional downstream target:
It describes gaps among represented integers and is not a prerequisite milestone for the capstone.
The criterion replaces a search for coordinates with a finite condition on the factorization of an input. It applies to composite integers as well as primes and distinguishes the exponent of a prime divisor from the mere presence of that divisor. The primitive obstruction also explains why a claim about coprime coordinates must not be confused with a claim that excludes all representations. The composition identity supplies an explicit statement of multiplicative closure, while the exercise gives a uniform restriction on consecutive runs. These are the consequences and accompanying results presented in Stein's treatment.
The mathematics is established, not an open research problem. Important formal ingredients already exist in Mathlib: the sum-of-two-squares development includes the arithmetic criterion and primitive obstruction, and the Diophantine approximation development supplies the bounded-denominator result. The work here is a source-aligned collection of exact theorem interfaces and independently checked proofs. Reusing those results does not claim a new proof of the classical mathematics or an exact transcription of Stein's argument.
A finite list of successful representations cannot establish an assertion about every positive integer. Similarly, a restriction on primitive representations is insufficient to settle general representability, because a nonprimitive pair is still a valid representation. The capstone must account for prime exponents and both directions of the equivalence simultaneously. The approximation result has its own coupled requirements: obtaining a small denominator without the stated error bound, or a good approximation with an uncontrolled denominator, does not meet the target. These distinctions are explicit in the source statements.
The namespace is SteinENT. Inputs use natural numbers, with positivity hypotheses wherever the source uses positive integers. Coordinates and all subtraction in the composition identity use integers. Prime exponents use Nat.factorization; primitivity uses Int.gcd x y = 1. Approximation witnesses use Lean's rational type, whose canonical numerator and positive denominator already express a reduced fraction. The error inequality is an inequality of real numbers. The gap exercise allows every integer starting point, including negative ones.
No hypothesis assumes the desired representation or restricts the capstone to a bounded test range. There is no additional definition that hides a proof obligation, and no separate alias item for primitivity. Standard Mathlib arithmetic, rational approximation, and tactic libraries provide reusable infrastructure. Complete alternative proofs are welcome when they preserve these interfaces, including the explicit positive-input boundary and unrestricted integer coordinates.
namespace SteinENT
theorem two_squares_criterion (n : ℕ) (hn : 0 < n) :
(∃ x y : ℤ, (n : ℤ) = x ^ 2 + y ^ 2) ↔
∀ p : ℕ, p.Prime → p ∣ n → p % 4 = 3 → Even (n.factorization p) := by sorry
end SteinENTLet be a positive integer. For a prime , let denote its exponent in the prime factorization of . Then
This gives a complete arithmetic criterion for representability, including composite integers.
Formalization Note The input and primes are natural numbers, with positivity explicit. Square coordinates are integers, and Nat.factorization records the prime exponent.
No open leaves. Every sub-goal is proved or awaiting decomposition.