Quadratic Residues and Nonresidues
Fix a prime number and ask a simple question. Which nonzero remainders modulo are perfect squares? For , squaring every nonzero residue gives . So only arise as squares, while never do. This split of the nonzero residues into "squares" and "non-squares" looks like an accident of arithmetic. But it turns out to be one of the most structured phenomena in elementary number theory. It is exactly half and half, always, for every odd prime. This partition matters because it governs the solvability of the humble equation . This in turn controls sums of two squares, the arithmetic of quadratic forms, primality tests, and the design of pseudorandom sequences. These sequences are used in cryptography and coding theory. Whether , , or is a square modulo dictates which primes divide numbers of the form , , or . This is exactly the kind of question that separates elementary from deep number theory. The big picture is that the multiplicative group of nonzero residues modulo is cyclic of even order . It is denoted . Squaring is a group homomorphism on this cyclic group. The image of a homomorphism from a cyclic group of even order onto itself has index exactly in the target. This single group-theoretic fact, once unpacked, is the entire reason quadratic residues split the nonzero residues exactly in half. This article gives the precise definition of quadratic residues and nonresidues modulo an odd prime. It proves the exact counting theorem: there are of each. It also builds the intuition — via the cyclic structure of — that will be packaged in later articles. These later articles are Euler's Criterion and The Legendre Symbol.
- A quadratic residue mod is a nonzero remainder that equals some ; a nonresidue is a nonzero remainder that never does.
- For any odd prime , exactly of the nonzero residues are quadratic residues, and are nonresidues.
- The proof uses that is cyclic and that squaring is exactly -to- on it.
- Zero is excluded from the definition by convention; it behaves separately and trivially ().
- The residue is a quadratic residue mod exactly when , a first hint of deeper reciprocity laws.
- This counting fact is the seed for Euler's Criterion and the Legendre symbol, which give computational tests for residue status.
- Key idea: squaring is a 2-to-1 group homomorphism on a cyclic group of even order, so the squares are precisely half the group.
Where this lives today
The exact halving of quadratic residues underlies Euler's Criterion, which computes residue status via , and the Legendre symbol, the -valued function that packages residue/nonresidue status algebraically and obeys the multiplicative law — both are direct successors to the counting fact proved here. This machinery feeds directly into Quadratic Reciprocity, which lets one decide residue status of large numbers without brute-force search. Quadratic residues also drive the quadratic sieve and other integer-factorization algorithms, where finding congruences with exploits exactly the residue structure explored above. In cryptography, the Rabin cryptosystem and quadratic-residuosity-based schemes (including Goldwasser–Micali probabilistic encryption) rest on the presumed hardness of distinguishing residues from nonresidues modulo a composite with unknown factorization. In coding theory and sequence design, Legendre sequences, built from the pattern of residues mod , give pseudorandom binary sequences with excellent autocorrelation properties, used in radar and spread-spectrum communications. Finally, the question of which primes represent , or divide values of , connects directly back to whether or is a residue mod — a thread that leads into the theory of Binary Quadratic Forms.
Ask the tutor: How does Euler's Criterion turn this counting fact into a fast test for whether a specific number is a quadratic residue modulo a large prime?
Let be an odd prime. An integer with is called a quadratic residue modulo if the congruence
has a solution . If no such exists, is called a quadratic nonresidue modulo . Since residue status depends only on the class of modulo , this partitions the set of nonzero residue classes
into two disjoint subsets: the quadratic residues and the quadratic nonresidues , with
The class is conventionally excluded from both sets: is always a square () but carries no information, since makes trivially solvable by .
Intuition
Think of under multiplication as a clock with hours, arranged (using a generator) in a cycle. Squaring sends each element to another element of the same cycle. Because the group is cyclic of even order , squaring behaves exactly like the map "double the exponent" on a cyclic group of even order — and doubling exponents modulo an even number hits exactly half the possible exponents, each exactly twice. Concretely, for the group has order and is generated by , since the powers of modulo are — all ten nonzero residues, confirming is a primitive root. Writing each nonzero residue as for , squaring corresponds to doubling modulo . The exponents for which can equal any given even target are exactly the even ; squaring an odd-exponent element gives an even exponent times two, which is even, and running over all , the squares only ever land on the elements with even exponent. There are five even exponents among , matching residues. A second way to see the same fact: the squaring map on satisfies , and since is odd, for . So the nonzero elements pair up into pairs , each pair giving a single square, and different pairs give different squares (this is the content of the theorem below, proved rigorously).
Let be an odd prime. Among the nonzero residue classes modulo , exactly are quadratic residues and exactly are quadratic nonresidues. Moreover, the quadratic residues are precisely the residue classes of
and these values are pairwise distinct modulo .
We use elementary properties of congruences and of the group (the nonzero residues under multiplication mod ), which is known to consist of exactly elements since is prime.
Step 1: Every quadratic residue is the square of some in . Let be a quadratic residue modulo , so for some integer with (if then , excluded since ). Reduce modulo to assume . If , we are done. Otherwise ; set . Then , and
So is also the square of . Either way, is congruent to the square of some integer in .
Step 2: The list is a complete list of the quadratic residues. By definition, each term in this list () is a quadratic residue (it is literally a square, and since as ). Step 1 shows every quadratic residue occurs among these values. Hence the set of values taken by equals exactly.
Step 3: These values are pairwise distinct modulo . Suppose and . Then
Since is prime, divides or divides . Now , so . Also (using and , so ), so either. This contradicts . Hence no such exist, and the squares are pairwise incongruent modulo .
Step 4: Conclusion. By Step 2, is exactly the set of residues of , and by Step 3 these are distinct residues. Hence
Since and partition the nonzero residues (every nonzero is either a square or not, by definition, with no overlap), we get
This completes the proof.
∎The proof technique here is direct counting via pairing () combined with a divisibility argument in a prime modulus (Step 3), avoiding any appeal to primitive roots or Euler's criterion — those are logically downstream results, not needed here, which keeps this proof self-contained and non-circular.
For an odd prime , the squaring map , , has image exactly , and every element of has exactly two preimages under , namely for some .
The image of is by definition of quadratic residue. If , then , so (as is prime) or . These give at most two distinct preimages, and ; they are distinct since is odd, so would force , impossible for (as is odd, is invertible mod ). Both do map to the same square: . Hence exactly two preimages for each element of . Since and each of the image points has exactly preimages partitioning the domain, , recovering as an alternative route to the theorem.
∎Worked examples
Example 1: . Here . Compute for :
So , a set of size as predicted, and , also size . Sanity check via the pairing : e.g. , and indeed — matches, both give . Also and — consistent. This confirms Step 3's pairing mechanism concretely: the squares of merely repeat the squares of in reverse order. Notice , and indeed , consistent with the later fact (a preview of quadratic reciprocity) that is a residue exactly when .
Example 2: — verifying non-multiplicativity of the naive residue count, and checking an asymmetric value. Here . Compute for :
So — count them: nine values, matching exactly. Now ask: is a quadratic residue mod ? Scan the list : is absent. So , and indeed one can confirm directly that no satisfies by noting is not among the computed squares, and every square mod appears in that list by Step 1 of the theorem (every pairs with to reduce to the range ). Is a residue? Yes — it is . So has the two solutions . Check: . Confirmed, and this exhibits the Corollary's "exactly two preimages" claim concretely with an asymmetric pair rather than a symmetric-looking small pair.
Take the composite modulus in place of an odd prime. The nonzero residues coprime to are ( of them). Squaring each:
Every unit modulo squares to . So among the units, only residue class () is a square, not . The clean "exactly half" count utterly fails: here it is , not . The reason the theorem's proof breaks down is visible directly in Step 3: the key divisibility step used that is prime, so that forces dividing one of the factors. For (composite, in fact a prime power ), we can have with dividing neither factor — e.g. : , divisible by , yet neither nor is divisible by . This is exactly why despite : the distinctness argument of Step 3 needs primality, and without it, squares can collapse onto far fewer than half of the units. (Separately, is even, so the pairing used in the intuition is also degenerate: pairs with , a different residue than pairs with , yet both pairs produce the same square — the pairs are disjoint but not square-distinguishing, which is precisely the multiplicity failure.)
Scope, edge cases & common traps
| Situation | What happens | Why |
|---|---|---|
| Every nonzero residue mod is just , and ; the "half-and-half" split is vacuous/degenerate | is odd, not even, so the group is trivial and the halving argument (needs odd) does not apply | |
| Excluded from by convention | is always a square but conveys no residue-splitting information | |
| Composite modulus | Residue counts among units need not be | Step 3 of the proof needs primality of the modulus; see Counterexample |
| Checking if is a QR by trial squaring | Correct but slow ( naively, using the pairing) | Faster criteria (Euler's Criterion, quadratic reciprocity) come later and avoid brute force |
| Confusing " has a square root mod " with " has a square root mod " | These are different questions (Hensel lifting needed for the latter) | The definition here is strictly mod a single prime , not a prime power |
The mathematician behind it
Origins · 1 minCarl Friedrich Gauss and how this was discovered
Gauss gave the first complete proofs of the Quadratic Reciprocity law in his 1801 Disquisitiones Arithmeticae, the book that turned quadratic residues from scattered curiosities into a structured theory. He returned to reciprocity repeatedly over his life, eventually publishing (or leaving) eight distinct proofs, each illuminating a different facet of the phenomenon — Gauss sums, lattice-point counting, and cyclotomy among them. His painstaking numerical tables of residues for small primes, computed by hand, informed the conjectural patterns he later proved.
Is ever called a quadratic residue?
By the standard convention used in this article and in most textbooks (Hardy–Wright, Ireland–Rosen), no: quadratic residues and nonresidues are defined only for with . The class is a perfect square () but including it would break the clean count of each, since it would leave an odd total of classes to split. Some authors informally say " is always trivially a square" as an aside, but it is not assigned to or . Always check which convention a given source uses before comparing counts.
Does the theorem say anything about which specific numbers are residues, or just the count?
Only the count, and a description of as the squares of . It does not tell you, for instance, whether is a residue mod a given prime without computing that range's squares directly. Determining residue status of a specific small number like , , or efficiently for large primes is exactly what Euler's Criterion and the quadratic reciprocity law are built to do, building on the group structure exposed in the proof here.
Why does being cyclic matter, even though the proof given avoids using it?
The proof given uses only elementary divisibility, deliberately avoiding the (true, but separately proved) fact that is cyclic, to keep this foundational result self-contained. However, cyclicity gives a second, illuminating proof: if is a primitive root mod , every nonzero residue is for a unique , and is a square exactly when is even, since is even, half the exponents are even. This is the "clock doubling" picture from the Intuition section, and it generalizes more easily to prime powers and other cyclic groups.
Is the number of quadratic residues ever not equal to for an odd prime?
No — the theorem proved above is unconditional for every odd prime , with no exceptions. The count holds for (giving residue, namely ), all the way up through arbitrarily large primes. What can vary from prime to prime is which specific numbers land in versus — that pattern is governed by quadratic reciprocity, not by this counting theorem.
How does this relate to solving in practice?
This theorem tells you how many 's admit solutions (exactly half), and the proof's Step 1–3 tell you that if a solution exists, it can be found among , with exactly two solutions overall () once one is found, by the Corollary. It does not by itself give an efficient algorithm to find for large ; that is the separate problem of modular square root extraction (e.g. Tonelli–Shanks), which uses Euler's Criterion first to check solvability.
Does the "exactly half" count generalize to prime powers or composite moduli?
Not in the same clean form. For an odd prime power , one can show the units mod still split with exactly squares among the units, because is still cyclic for odd . But for with , the group is not cyclic, and the fraction of squares among units is , not — exactly the phenomenon shown in the Counterexample block for . General composite requires the Chinese Remainder Theorem to combine residue conditions prime-by-prime, and the resulting fraction of residues is generally smaller than .