Contents / Number Theory / Modular Arithmetic
Chapter 2
Modular Arithmetic
Congruence as an equivalence relation compatible with the arithmetic; Z/nZ as a ring, a field exactly for prime n; inverses via the extended Euclidean algorithm and the full linear-congruence theorem; fast modular exponentiation; the Chinese Remainder Theorem with its explicit formula and the non-coprime case; quadratic residues, Euler's criterion, the Legendre symbol and when -1 is a square; and the Jacobi symbol with Solovay-Strassen.
Introduction
Arithmetic modulo is what remains of the integers after you agree to stop distinguishing numbers that differ by a multiple of . That single act of forgetting is astonishingly productive. It turns an infinite ring into a finite one, so questions that are open over become finite checks; it turns the multiplicative structure of into a finite abelian group, so Lagrange's theorem applies and exponents start to repeat; and it turns divisibility statements into equations, which can be added, multiplied and solved.
The chapter is built in four movements. The first develops the congruence relation itself: that it is an equivalence relation compatible with addition and multiplication, that a residue is invertible exactly when it is coprime to the modulus, that the inverse is produced by the extended Euclidean algorithm, and that powers can be computed in logarithmically many multiplications. The second is the Chinese Remainder Theorem, proved with existence, uniqueness and an explicit reconstruction formula, and then pushed past its usual hypothesis to say exactly when a system with non-coprime moduli is solvable. The third recasts everything structurally: is a quotient ring, congruence is the congruence relation attached to the ideal , and the Chinese Remainder Theorem becomes an isomorphism of rings.
The fourth movement asks a question the first three cannot answer: which residues are squares? Linear equations mod are settled completely by the Euclidean algorithm, but is a genuinely different problem, and the theory that answers it — Euler's criterion, the Legendre symbol, quadratic reciprocity and its computational cousin the Jacobi symbol — is the first place in elementary number theory where the answer is deep rather than merely careful.
Throughout, and denote positive integers, denotes a prime, and is always taken to be non-negative. Divisibility and the Euclidean algorithm are assumed, as developed in Divisibility and Primes; the language of rings and ideals is used from the third movement onward, but the arithmetic never depends on it.
2.1Congruences and residue classes
The definition is a piece of deliberate economy: instead of recording a number, record only what it leaves behind after division by .
Definition 2.1 (Congruence modulo ). Let be a positive integer. For integers and we write
and say is congruent to modulo , when , that is, when for some integer . The integer is the modulus.
Everything in this chapter follows from the fact that this relation is an equivalence relation and that it respects the ring operations. Neither half is automatic: the relation " and have the same number of digits" is an equivalence relation that respects nothing, and the relation " " respects addition but is not an equivalence relation.
Theorem 2.2 (Congruence is an equivalence relation). For each fixed , congruence modulo is reflexive, symmetric and transitive on .
Proof. Reflexive: , so .
Symmetric: if then , and is an integer.
Transitive: if and , then adding gives , and is an integer.
Each step used only that the multiples of form a set containing , closed under negation and closed under addition.∎
Definition 2.3 (Residue class). The equivalence class of under congruence modulo is the residue class
written or when is clear. The set of all residue classes modulo is denoted .
Proposition 2.4 (Least residues classify the classes). Every integer is congruent modulo to exactly one of . Consequently has exactly elements, and if and only if and leave the same remainder on division by .
Proof. Division with remainder gives unique integers with and . Then , so , which proves that at least one such representative exists.
For uniqueness, suppose with . Then while , and no positive multiple of is smaller than . So cannot be a multiple of , a contradiction.
Finally, if and with , then , so exactly when , which by the uniqueness just proved happens exactly when .∎
The number produced here is the least nonnegative residue of modulo , written . Note the typographical distinction that will be kept throughout: is a number, the remainder; is a statement about two numbers.
Intuition. A clock face is the whole of the definition. The hour hand at o'clock and at o'clock points at the same place, because the two readings differ by one full revolution. Arithmetic mod is what a clock does: you may add and multiply hour-readings and the answer is again a reading, because a whole number of revolutions added anywhere along the way changes nothing about where the hand ends up.
The reason congruence is worth a symbol as heavy as , rather than being phrased as a divisibility statement each time, is that it can be manipulated like an equation.
Theorem 2.6 (Congruence is compatible with the ring operations). Fix and suppose and . Then
Proof. Write and .
For the sum, . For the difference, replace by .
For the product, the trick is to change one factor at a time:
and is an integer. Hence .∎
Corollary 2.7 (Polynomials respect congruence). Let be a polynomial with integer coefficients. If then .
Proof. By the theorem Congruence is compatible with the ring operations and induction on , implies for every : the case is , and . Multiplying by the integer coefficient gives , and summing the finitely many terms gives .∎
This corollary is the engine behind every elementary divisibility test. Since , any integer written as satisfies : a number is congruent to its digit sum mod . Since , the same argument gives the alternating digit sum mod .
Example 2.8 (A divisibility test from a congruence). Determine and without dividing.
Solution.
- Mod : since , the number is congruent to its digit sum, .
- Repeat on : , then . So .
- Mod : since , the number is congruent to the alternating sum of its digits taken from the units end, .
- And . So the two remainders are and .
Example 2.9 (Ruling out a solution by choosing a modulus). Show that has no solution in integers.
Solution.
- Work modulo . By the corollary Polynomials respect congruence it is enough to know the squares of the four residue classes: , , , .
- So every square is or , and a sum of two squares is or — never .
- But , so .
- No solution exists. The whole argument is a finite check on four residue classes, replacing a search over infinitely many integers.
Definition 2.10 (Complete residue system). A set of integers is a complete residue system modulo if every integer is congruent modulo to exactly one .
Proposition 2.11 (Recognising a complete residue system). A set of integers is a complete residue system modulo if and only if its elements are pairwise incongruent modulo . Moreover, if is a complete residue system and , then so is for any integer .
Proof. If the elements are pairwise incongruent they meet distinct classes, and there are only classes by the proposition Least residues classify the classes, so they meet every class exactly once. The converse is immediate from "exactly one".
For the second claim, suppose . Subtracting and using compatibility, , so . Since , Euclid's lemma gives , hence . So the new elements are pairwise incongruent, and the first claim applies.∎
2.2Modular inverses and linear congruences
Addition, subtraction and multiplication descend to residue classes. Division does not, and the question of exactly when it does is the first genuinely arithmetic question of the chapter.
Definition 2.12 (Modular inverse). An integer is invertible modulo (a unit mod ) if there is an integer with
Such an is an inverse of modulo , written .
Theorem 2.13 (Invertibility criterion). Let . An integer is invertible modulo if and only if . When it is, an inverse is obtained from any Bézout identity by taking .
Proof. () Suppose . By Bézout's identity there are integers with . Reducing modulo kills the term , leaving , so is an inverse.
() Suppose , so for some , that is . Let . Then divides both and , hence divides their difference , so .∎
The proof is not merely an existence statement: the extended Euclidean algorithm computes the Bézout coefficients explicitly and in steps, so the inverse is as cheap as the gcd.
Corollary 2.14 (Inverses are unique modulo ). If is invertible modulo , its inverse is unique modulo .
Proof. Suppose and . Then
where each congruence used compatibility of multiplication with congruence.∎
Method 2.15 (Inverting modulo by the extended Euclidean algorithm).
- Run the Euclidean algorithm on , recording each quotient.
- If the last nonzero remainder is not , then and no inverse exists; stop.
- Otherwise back-substitute to write .
- Reduce modulo to the least nonnegative residue. That is .
- Check: multiply out and confirm the remainder is .
Example 2.16 (An inverse by back-substitution). Compute .
Solution.
- Euclid: , then , then . The gcd is , so the inverse exists.
- Back-substitute from the second-to-last line: .
- Substitute : .
- So , giving .
- Check: . ✓
Example 2.17 (When the inverse does not exist). Does have an inverse modulo ?
Solution.
- , so by the theorem Invertibility criterion there is no inverse.
- To see the failure concretely, note is always a multiple of modulo , since is too: every value of lies in , and is not among them.
With inverses in hand, the general linear congruence can be solved completely — including the case where no inverse exists.
Theorem 2.18 (Linear congruence). Let be integers, , and . The congruence
has a solution if and only if . In that case the solutions form exactly residue classes modulo : if is one solution, they are
equivalently a single residue class modulo .
Proof. The congruence says for some , i.e. that lies in the set of integer combinations of and . That set is exactly , so a solution exists if and only if .
Assume and write , , with . Dividing the relation through by shows that is equivalent to
Since , the theorem Invertibility criterion gives mod , and multiplying by it shows the solution set is the single class — in particular non-empty, and a class modulo .
Finally, one class modulo splits into exactly classes modulo , namely those listed, because two integers congruent mod differ by a multiple of , and the multiples of fall into exactly classes mod , represented by .∎
Example 2.19 (A linear congruence with several solutions). Solve .
Solution.
- and , so by the theorem Linear congruence there are exactly solution classes mod .
- Divide through by : .
- Invert mod : , so .
- Then .
- Lifting to modulus : or .
- Check: , and . ✓
Pitfall. You may not "cancel" a common factor from both sides of a congruence while leaving the modulus alone. From , cancelling gives the false . The correct cancellation law is: if then . Cancelling from the modulus as well is only free when .
2.3Powers, orders and fast exponentiation
A quantity like is hopeless to compute by first forming , an integer with over eight hundred digits. Two ideas make it trivial: reduce after every multiplication, and never perform more than multiplications.
Theorem 2.20 (Correctness of square-and-multiply). Let have binary expansion with and . Define and, for down to ,
Then , and the computation uses at most modular multiplications of numbers below .
Proof. Let be the integer whose binary digits are the leading bits of . Then and , and .
We show by downward induction that . The base case is . If , then since congruence is compatible with multiplication, and ; in both cases the recursion yields . Taking gives .
Each of the steps costs one squaring and at most one further multiplication, so at most multiplications; and since each is reduced below , every product formed is below .∎
Reducing at every step is the whole point: it is what keeps the operands small, and by the theorem Congruence is compatible with the ring operations it changes no answer.
Example 2.21 (Square-and-multiply in practice). Compute .
Solution.
- Write , so the bit string is .
- Start . Bit : .
- Bit : . Bit : .
- Bit : . Bit : . Bit : .
- So . Six squarings and two extra multiplications replaced ninety-nine.
Once exponents are cheap, the question becomes which exponents matter. Powers of a fixed unit must eventually repeat, because there are only finitely many residues, and the first repeat happens at .
Definition 2.22 (Multiplicative order). Let . The order of modulo , written , is the least positive integer with .
Proposition 2.23 (The order divides every exponent that works). Let and . Then for a positive integer ,
In particular if and only if .
Proof. If , say , then .
Conversely suppose . Divide: with . Then
If this contradicts minimality of , so and . For the last claim, assume and multiply by , which exists because is a unit, to get .∎
Theorem 2.24 (Fermat's little theorem). Let be prime and . Then
Proof. Since we have , so by the proposition Recognising a complete residue system the list is pairwise incongruent mod , and none is . Hence it is a permutation of modulo . Multiplying the two lists together,
Each of is coprime to , so is a unit mod and may be cancelled, leaving . Multiplying by gives , which also holds when since then both sides are .∎
Corollary 2.25 (Euler's theorem). If then , where counts the integers in coprime to . Consequently .
Proof. Let be the least residues coprime to . Multiplication by permutes them: each is coprime to (a product of two units is a unit), and forces after multiplying by . Multiplying all of them,
and the product of units is a unit, so it cancels. The divisibility statement is then the proposition The order divides every exponent that works.∎
Example 2.26 (Reducing an exponent with the order). Compute and the order of modulo .
Solution.
- The powers of mod are , so .
- By the proposition The order divides every exponent that works, only matters, and .
- Hence .
Pitfall. Euler's theorem reduces the exponent modulo , not modulo , and only when . Computing as is wrong twice over: , and the true value is only by coincidence — the same false step gives , whereas .
2.4The ring
The theorem Congruence is compatible with the ring operations says precisely that the operations of descend to residue classes. Naming the resulting object is what lets the rest of algebra be applied to it.
Definition 2.27 (The ring of residues). On the set of residue classes modulo , define
Theorem 2.28 ( is a commutative ring). The operations above are well defined, and with them is a commutative ring with zero and identity .
Proof. Well defined: the definitions refer to representatives, so we must check the answer does not depend on which ones. If and then and , so by the theorem Congruence is compatible with the ring operations and , i.e. and .
Axioms: each is inherited. For instance associativity of multiplication reads , where the middle equality is associativity in . Commutativity, distributivity, the additive identity , additive inverses and the multiplicative identity all transfer by the same one-line argument, because every axiom is an identity between polynomial expressions in the elements.∎
Theorem 2.29 (The group of units). The invertible elements of are exactly the classes with . They form a group under multiplication, of order .
Proof. The description of the units is the theorem Invertibility criterion restated, noting that depends only on : if then , and any common divisor of and divides , and conversely.
The units of any commutative ring form a group: is a unit; if are units then , so is; and is a unit with inverse . Associativity is inherited. Counting: the classes coprime to are represented by the integers in coprime to , and there are of those.∎
Theorem 2.30 ( is a field exactly when is prime). For , the following are equivalent: is a field; is an integral domain; is prime.
Proof. Prime field. If is prime, every with satisfies , hence , hence is a unit by the theorem The group of units. So every nonzero element is invertible and because .
Field domain. In any field, if with , multiplying by gives .
Domain prime. If is composite, write with . Then and are nonzero — neither nor is divisible by , being smaller and positive — while . So has zero divisors and is not a domain.∎
Notation. When is prime the field is written . Its multiplicative group has order , which is the group-theoretic content of Fermat's little theorem: the order of an element divides the order of the group.
Example 2.31 (Zero divisors and units modulo ). List the units and the zero divisors of , and solve .
Solution.
- Units: the with , namely — four of them, and indeed .
- Each is its own inverse here: , , .
- Zero divisors: the nonzero non-units ; for instance and .
- Testing all twelve classes, holds for : four solutions to a quadratic. Over a field that is impossible, and the theorem is a field exactly when is prime explains why it is possible here.
2.5The Chinese Remainder Theorem
A congruence modulo is a piece of partial information about an integer. The Chinese Remainder Theorem says that when the moduli are pairwise coprime, several such pieces fit together in exactly one way — no consistency conditions to check, and no ambiguity beyond the obvious one.
Theorem 2.32 (Chinese Remainder Theorem). Let be pairwise coprime positive integers, that is whenever , and set . For any integers the system
has a solution, and the solution is unique modulo .
Proof. Uniqueness. Suppose and both satisfy the system. Then for every . Since the are pairwise coprime, their product divides : inductively, if and both divide and are coprime — because is coprime to each factor — then their product divides . Hence , i.e. .
Existence. Consider the map
which is well defined because , so implies . Uniqueness, just proved, says is injective. Both sides are finite sets of the same size , so an injective map between them is surjective. Surjectivity is exactly the assertion that every tuple is achieved, i.e. that the system has a solution.∎
The counting argument is short but gives no number. The next section repairs that with an explicit formula; the pigeonhole version is worth keeping because it is the one that generalises, and because it makes clear that existence and uniqueness are not two facts but one.
Example 2.33 (Two congruences by inspection). Solve and .
Solution.
- The moduli are coprime, so by the theorem Chinese Remainder Theorem there is exactly one solution class mod .
- List the integers below : .
- Reduce each mod : . The value occurs at .
- So . Searching the five candidates rather than all thirty-five is the small-scale version of the constructive method below.
2.6The constructive formula
To solve the system rather than merely know it is solvable, build a residue that is at one coordinate and at all the others, then take the obvious combination.
Theorem 2.34 (Explicit CRT reconstruction). With pairwise coprime and , put
Each exists, and
is the unique solution of the system .
Proof. First, exists: is the product of the with , each coprime to , so and the theorem Invertibility criterion applies.
Now fix and reduce the sum modulo . For the factor contains as a divisor, so and every such term drops out. Only the term survives, and there by the choice of . Hence
so the stated solves the system. By the theorem Chinese Remainder Theorem it is the only solution mod .∎
The elements deserve a name: they satisfy and for , so they behave like a basis of indicator functions, and the formula reads as "assemble from its coordinates". They depend only on the moduli, so they are computed once and reused for every right-hand side.
Method 2.35 (Solving a CRT system).
- Check the moduli are pairwise coprime; if not, use the theorem Systems with non-coprime moduli instead.
- Form and each .
- Invert modulo by the extended Euclidean algorithm to get .
- Form and reduce modulo .
- Verify by reducing the answer modulo each in turn.
Example 2.36 (Three congruences by the formula). Solve , , .
Solution.
- , and , , .
- Inverses: and , so . , so . , so .
- Assemble: .
- Reduce: , so .
- Check: , , . ✓
2.7Systems with non-coprime moduli
Drop the coprimality hypothesis and both halves of the theorem change. Solutions need no longer exist — and is contradictory, since the second forces even — and when they do exist, uniqueness is modulo the least common multiple rather than the product.
Theorem 2.38 (Two congruences, general moduli). Let be positive integers and . The system
has a solution if and only if . When it does, the solution is unique modulo .
Proof. Necessity. If solves both, then and , so divides .
Sufficiency. The first congruence says for some integer . Substituting into the second, we need
By the theorem Linear congruence this is solvable in exactly when divides , which is the hypothesis, and then is determined modulo . Writing gives
so is determined modulo .∎
Corollary 2.39 (General systems). A system , , has a solution if and only if
and the solution is then unique modulo .
Proof. Necessity is the necessity half of the theorem Two congruences, general moduli applied to each pair.
For sufficiency, induct on . The case is trivial. Suppose the first congruences have been merged into a single congruence with ; note for each . We must merge it with , so by the theorem Two congruences, general moduli it suffices to show divides .
Let be any prime power dividing exactly, say . Then and , and since is a least common multiple, for some . Hence , which divides by hypothesis; and . Adding, . As this holds for every prime power exactly dividing , we get . The merged modulus is .∎
Example 2.40 (A solvable system with overlapping moduli). Solve and .
Solution.
- and , which is divisible by . So a solution exists, unique mod .
- Write and substitute: , so .
- Divide through by : , so .
- Take : . Hence .
- Check: and . ✓
Example 2.41 (An unsolvable system). Show that , has no solution.
Solution.
- and , which is divisible by — so this pair alone is fine, and in fact works.
- Adjoin a third congruence . Now must divide ✓, but must divide , and it does not.
- By the corollary General systems the three-congruence system is unsolvable, even though every pair but one is compatible. Pairwise checking is genuinely necessary, and checking only consecutive pairs is not enough.
2.8The isomorphism and its applications
The Chinese Remainder Theorem is a statement about sets of solutions, but the map in its proof respects addition and multiplication, and that upgrade is what makes the theorem a computational tool rather than a curiosity.
Theorem 2.42 (CRT as a ring isomorphism). If then the reduction map
is an isomorphism of rings, where the right-hand side has componentwise operations. More generally for pairwise coprime with product .
Proof. is well defined and bijective by the theorem Chinese Remainder Theorem. It is a ring homomorphism because reduction is: and by the definition of the operations in , and the same in the second coordinate, while is the identity of the product. A bijective ring homomorphism is an isomorphism, its set-theoretic inverse being automatically a homomorphism.∎
Corollary 2.43 ( is multiplicative). If then .
Proof. An isomorphism of rings carries units to units bijectively, so . A pair is invertible componentwise exactly when both components are, so the units of a product are the product of the unit groups. Counting with the theorem The group of units gives .∎
The practical consequence is a divide-and-conquer strategy: to compute something modulo , compute it modulo each prime power dividing — where the arithmetic is small and the structure is simpler — and reassemble.
Example 2.44 (A large power by splitting the modulus). Compute .
Solution.
- with , so work in .
- Mod : and , so .
- Mod : and , so .
- Reassemble: solve , . The candidates are , and .
- So .
Example 2.45 (Counting square roots of ). How many solutions does have?
Solution.
- , so by the theorem CRT as a ring isomorphism, corresponds to a triple and holds exactly when it holds in each coordinate.
- Modulo an odd prime , means , so : two solutions each.
- The triples therefore number , so there are solutions mod .
- Explicitly they are — and note , another reminder that a quadratic over a non-field can have many roots.
2.9Congruence as an equivalence relation
It is worth pausing over what the theorem Congruence is an equivalence relation buys, because the payoff is structural rather than computational: an equivalence relation is the same thing as a partition, and a partition is the same thing as a new set whose points are the old set's classes.
Theorem 2.46 (Classes partition the integers). Fix . The residue classes modulo are non-empty, pairwise disjoint, and their union is . Moreover if and only if .
Proof. Each class is non-empty since by reflexivity, and the union is all of for the same reason.
Suppose , say lies in both. Then and , so by symmetry and transitivity . Now for any we get , so , and symmetrically; hence . So two classes are either disjoint or equal.
Finally, if then , which by the previous paragraph forces ; conversely gives , i.e. .∎
Definition 2.47 (Quotient set and canonical projection). The set of classes is the quotient , and the canonical projection is
The projection is surjective by construction and is very far from injective: each fibre is an infinite arithmetic progression of common difference . What the theorem Least residues classify the classes adds is a canonical choice of representative in each fibre, which is what makes the quotient a computable object and not merely a well-defined one.
Remark. Choosing representatives is a convention, not a fact. The least nonnegative residues are standard, but the least absolute residues are often better: modulo they let one write , turning into a one-line observation. Both are complete residue systems, so nothing mathematical distinguishes them.
Example 2.48 (Least absolute residues shorten a computation). Compute and using signed representatives.
Solution.
- , so .
- , so .
- By Fermat's little theorem , so .
- Hence and .
2.10Congruence as a ring congruence
Abstract algebra isolates the property that made possible, and gives it a name.
Definition 2.49 (Ring congruence). An equivalence relation on a ring is a congruence relation if it is compatible with the operations: whenever and ,
By the theorem Congruence is compatible with the ring operations, congruence mod is a congruence relation on . The next result says that on there are no others.
Theorem 2.50 (Every congruence on is a congruence mod ). Let be a congruence relation on the ring . Then there is a unique integer such that , where gives equality.
Proof. Let . Then ; if then by compatibility with addition and negation, so ; and if and then , so . Thus is closed under subtraction and under multiplication by arbitrary integers.
If take . Otherwise let be the least positive element of , which exists by well-ordering since contains a nonzero element and is closed under negation. For any , divide: with ; then , so minimality forces and . Conversely every multiple of is in . Hence .
Finally if and only if — subtract from one side, add it back for the converse — if and only if , which is . Uniqueness holds because is recovered from as the least positive element of .∎
Remark. The set built in that proof is exactly an ideal, and the correspondence "congruence relations ideals" holds in every ring, not just . What is special about is the division algorithm, which forced to be generated by one element.
2.11Ideals, divisibility and containment
Definition 2.51 (Ideal of ). A subset is an ideal if , is closed under subtraction, and for every and . The principal ideal generated by is .
Theorem 2.52 ( is a principal ideal domain). Every ideal of equals for a unique .
Proof. This is the middle paragraph of the proof of the theorem Every congruence on is a congruence mod , which used only the three closure properties now taken as the definition: if take , otherwise take least positive in and divide with remainder to show . Uniqueness: with forces and , hence .∎
Proposition 2.53 (Divisibility is containment). For positive integers :
Moreover and .
Proof. If then every multiple of is a multiple of , giving containment. Conversely if then , so .
The set is an ideal, so by the theorem is a principal ideal domain it equals for some . Since and lie in it, divides both; and for some , so every common divisor of and divides . That is the defining property of the greatest common divisor. The intersection is likewise an ideal , and is a common multiple divided by every common multiple, i.e. the least one.∎
Remark. "To contain is to divide" inverts the intuition that a divisor is smaller. The bigger ideal belongs to the smaller number: because . Bézout's identity, the Chinese Remainder Theorem and the classification above are all statements about the lattice of these ideals.
2.12Homomorphisms and induced maps
Theorem 2.54 (First isomorphism theorem for ). The projection is a surjective ring homomorphism with kernel . Conversely, if is a ring homomorphism whose kernel contains , then factors uniquely as for a ring homomorphism .
Proof. That is a homomorphism is the definition of the operations on classes; it is surjective because every class is for some ; and means , so the kernel is .
For the factorisation, there is no choice about : the requirement forces , which gives uniqueness. It is well defined: if then , so . And it is a homomorphism because , similarly for products, and .∎
Corollary 2.55 (When reduction descends). For positive integers there is a ring homomorphism sending if and only if , and it is then the unique ring homomorphism between them.
Proof. () If then by the proposition Divisibility is containment, so the theorem First isomorphism theorem for applies to and produces the map.
() If such a map exists then , but and , so . Uniqueness: any ring homomorphism out of is determined by the image of , which must be the identity, and .∎
Example 2.56 (A congruence pulled back along reduction). Given that , what can be said about modulo , and modulo ?
Solution.
- , so the corollary When reduction descends gives a well-defined reduction: .
- , so no such map exists and the class of mod is genuinely not determined: gives , while gives .
- What is determined is modulo — consistent with step 1.
2.13Quadratic residues
Linear congruences are settled: is solved by the Euclidean algorithm. The quadratic case is not settled by anything so mechanical, and deciding solvability without searching is the subject of the rest of the chapter.
Definition 2.57 (Quadratic residue). Let be an odd prime and . Then is a quadratic residue modulo if has a solution, and a quadratic non-residue otherwise. The set of residues is written .
The class of is excluded by convention: always has the single solution , and including it would break every counting statement below.
Theorem 2.58 (Exactly half the nonzero residues are squares). Let be an odd prime. Of the nonzero classes mod , exactly are quadratic residues and exactly are non-residues. The residues are listed without repetition by .
Proof. The squaring map , , has image by definition. Each fibre has exactly two elements: if then , and since is a field by the theorem is a field exactly when is prime, or . These two possibilities are distinct because would give , impossible for odd and .
So is two-to-one onto its image, and ; the remaining classes are non-residues. Since and have the same square, the values already cover the image, and they are pairwise distinct because gives .∎
Example 2.60 (The residues modulo ). List the quadratic residues mod and decide whether and are solvable.
Solution.
- By the theorem Exactly half the nonzero residues are squares it suffices to square through : .
- So , five of the ten nonzero classes, and the non-residues are .
- , so is solvable, with .
- , so has no solution.
2.14Euler's criterion
Listing squares decides membership in in work. Euler's criterion decides it in multiplications, by turning a question about solvability into a single modular power.
Theorem 2.61 (Euler's criterion). Let be an odd prime and . Then
Proof. First, by Fermat's little theorem, so divides where . As is a field, : the value is always one of the two.
If , write with . Then
again by Fermat's little theorem. So every residue gives .
It remains to show no non-residue gives . The congruence is a polynomial equation of degree over the field , so it has at most solutions. The theorem Exactly half the nonzero residues are squares supplies exactly solutions already, namely the quadratic residues. There is no room for another, so the non-residues must give .∎
Example 2.62 (Euler's criterion on a larger prime). Is a quadratic residue modulo the prime ?
Solution.
- Compute by squaring: , .
- , and .
- Then .
- By the theorem Euler's criterion, is a quadratic residue mod . Indeed .
2.15Counting solutions, and as a residue
Proposition 2.63 (A square congruence has or solutions). Let be an odd prime and . Then has exactly two solutions if and none otherwise; the two solutions are and .
Proof. If there is no solution the count is by definition. If is a solution then so is , and these are incongruent because would give , impossible. There are no others: gives , and being a field forces .∎
Theorem 2.64 (The first supplement). Let be an odd prime. Then is a quadratic residue modulo if and only if . Equivalently,
Proof. By the theorem Euler's criterion with , the class is a quadratic residue exactly when . Since is odd and , the integers and are incongruent mod , so the congruence holds exactly when the integer equals , i.e. when is even.
Write as or , the only odd options. If then is even; if then is odd. So the condition is precisely .∎
Corollary 2.65 (Square roots of modulo a prime). For the congruence has exactly two solutions, and one of them is modulo . For it has none.
Proof. The counts follow from the theorem The first supplement together with the proposition A square congruence has or solutions.
For the explicit root, put and pair each factor of with its negative: , so
Wilson's theorem states . When , is even, so .∎
Example 2.66 (Building a square root of ). Find a solution of .
Solution.
- , so by the theorem The first supplement a solution exists.
- The corollary Square roots of modulo a prime offers .
- Reduce: , so .
- Check: . ✓ The other solution is .
Remark. The first supplement is the engine of Fermat's two-square theorem: a prime is a sum of two squares, and the proof begins by choosing with — which exists for exactly these primes. It also settles which primes divide some value of , a question that looks unrelated to congruence classes until this theorem is available.
2.16The Legendre symbol
Euler's criterion computes a value in that records whether is a square. Packaging that value as a symbol turns quadratic-residue statements into algebraic identities.
Definition 2.67 (Legendre symbol). Let be an odd prime. For an integer , define
Proposition 2.68 (Euler's criterion in symbol form). For every integer and odd prime ,
and the symbol depends only on .
Proof. If both sides are . Otherwise the congruence is the theorem Euler's criterion, read through the definition of the symbol. Dependence on only: if then by the corollary Polynomials respect congruence, and two values in that are congruent mod an odd prime are equal.∎
2.17Multiplicativity of the Legendre symbol
Theorem 2.69 (The Legendre symbol is completely multiplicative). For an odd prime and all integers ,
Proof. By the proposition Euler's criterion in symbol form,
Both sides lie in , and distinct members of that set are incongruent modulo an odd prime — the largest difference is . So the congruence is an equality.∎
Corollary 2.70 (Products of residues and non-residues). For : residue times residue is a residue, residue times non-residue is a non-residue, and non-residue times non-residue is a residue. Also whenever .
Proof. Each statement is the multiplication table of read through the theorem The Legendre symbol is completely multiplicative: , , . Taking gives .∎
Pitfall. That two non-residues multiply to a residue is the standard surprise, and it kills the naive analogy with divisibility. It also means the Legendre symbol cannot be used to find a square root: knowing tells you a root exists and nothing about where it is.
Example 2.71 (Factoring a symbol). Evaluate .
Solution.
- Reduce first: , so by the proposition Euler's criterion in symbol form.
- Factor and split by multiplicativity: , since by the corollary Products of residues and non-residues.
- The squares mod are , and is not among them, so .
- Hence .
2.18Quadratic reciprocity: a preview
Multiplicativity reduces to the symbols of the prime factors of . What it does not do is evaluate for a prime ; the numerator and denominator are unrelated as far as anything proved so far can tell. Quadratic reciprocity asserts that they are not unrelated at all — it is proved in the following chapter, and stated here because every computation below uses it.
Theorem 2.72 (Law of quadratic reciprocity). Let be odd primes. Then
Equivalently, unless , in which case . The two supplements are
so is a quadratic residue modulo exactly when .
The first supplement is already proved above as the theorem The first supplement; the second and the reciprocity law itself are not proved here.
Method 2.73 (Evaluating by reciprocity).
- Reduce modulo to a least residue, using signed representatives if that shortens the factorisation.
- Factor the numerator and split the symbol by multiplicativity.
- Handle and by the supplements; drop any square factor, whose symbol is .
- For an odd prime in the numerator, flip to , inserting a sign only when .
- Reduce modulo and recurse. The numbers shrink, so the process terminates.
Example 2.74 (Reciprocity in action). Evaluate .
Solution.
- , so no sign appears when flipping: .
- Reduce: , so this is .
- Both and are , so flipping costs a sign: .
- Split: . Now so ; and , so .
- So and : is a quadratic residue mod .
2.19The Jacobi symbol
The recipe above has an ugly step: reducing modulo produces a composite numerator, which must be factored before reciprocity can be applied again, and factoring is expensive. The Jacobi symbol removes the need to factor by extending the definition to composite denominators.
Definition 2.75 (Jacobi symbol). Let be an odd positive integer with prime factorisation (repetitions allowed in the exponents). For any integer define
a product of Legendre symbols. By convention .
Pitfall. When is prime the Jacobi symbol is the Legendre symbol, so the notation is consistent. But for composite , does not mean is a square mod . Modulo , , while the squares mod are and is not among them. Two non-residues cancelled. The implication that survives is one-directional: if is a square mod then , so the value is a genuine proof of non-residuity.
2.20Properties of the Jacobi symbol
Every formal rule of the Legendre symbol survives the extension — which is exactly why the extension is useful, since the rules can now be applied without factoring.
Theorem 2.76 (Jacobi symbol rules). Let be odd positive integers and integers. Then
the symbol depends only on , and exactly when . Moreover the supplements and reciprocity hold verbatim:
for .
Proof. Multiplicativity in the numerator is the theorem The Legendre symbol is completely multiplicative applied factor by factor; multiplicativity in the denominator is immediate from the definition, since the prime factorisation of is the concatenation of those of and . Dependence on follows because , so determines . The value is exactly when some , i.e. when .
For the supplements and reciprocity, the point is a parity computation. If are odd then
since is divisible by . So is additive over products of odd numbers; the same computation with shows it too is additive, using . Writing as a product of primes with repetition, by additivity, and likewise for . Reciprocity follows by applying the Legendre law to every pair of primes from and and collecting the exponent by the same additivity.∎
Example 2.77 (A Jacobi computation with no factoring). Evaluate .
Solution.
- and , so flipping is free: .
- Reduce: , giving .
- , so .
- , so flip freely: , using .
- , so ; then gives .
- , and so this is .
- Hence . At no point was any number factored.
2.21Jacobi symbols and primality testing
The Jacobi symbol is computable in bit operations by the recipe above, without factoring . Euler's criterion says that for a prime the symbol agrees with a modular power. Comparing the two quantities therefore tests primality.
Definition 2.78 (Euler witness and Euler pseudoprime). Let be an odd composite number and . Then is an Euler witness for if
If instead the congruence holds, is an Euler pseudoprime to base and is a liar.
Proposition 2.79 (The test is sound). If is an odd prime then for every . Hence an Euler witness proves composite.
Proof. For prime the Jacobi symbol is the Legendre symbol, and the congruence is the proposition Euler's criterion in symbol form. The contrapositive is the second sentence: if some fails the congruence, cannot be prime.∎
Theorem 2.80 (At least half the bases are witnesses). Let be odd and composite. Then the set of liars
is a proper subgroup of . Consequently , so a base chosen uniformly at random from is an Euler witness with probability at least .
Proof. is a subgroup: the map is a homomorphism , because both and the Jacobi symbol are multiplicative by the theorem Jacobi symbol rules, and is exactly its kernel.
That is proper is the substantive half, and we show it in the two shapes a composite can take. If is divisible by the square of a prime, say , take ; then has order divisible by in while has order dividing , and is odd, so . Otherwise with prime, and ; choose a non-residue mod and, by the theorem Chinese Remainder Theorem, an with and . Then , whereas , so is impossible. Hence .
A proper subgroup has index at least by Lagrange's theorem, so , and the bases in that are not in — including every with , which fail immediately — number at least .∎
This is the Solovay–Strassen primality test: pick random bases, declare composite the moment a witness appears, and otherwise declare it prime with error probability at most . Unlike the Fermat test, it has no analogue of the Carmichael numbers — there is no composite for which every coprime base lies — and that is exactly what the theorem above rules out.
Example 2.81 (Detecting a composite with one base). Use the base to show is composite, given that passes the Fermat test to base .
Solution.
- , and indeed , so the Fermat test learns nothing: is a Carmichael number.
- Jacobi: . Since , this is .
- Modular power: . Squaring gives , , , so the order of divides ; then gives .
- Here the two agree, so is a liar. Try : , while .
- Since , the base is an Euler witness and is composite. The theorem At least half the bases are witnesses guarantees such a base is easy to find — unlike for the Fermat test, where for almost none exists.
- Cancelling a factor without touching the modulus. From one may only conclude . Cancelling from gives the false ; the true conclusion is .
- Inverting when no inverse exists. Writing presumes . When the congruence has solutions or none, and the theorem Linear congruence is the correct tool.
- Reducing an exponent modulo . Exponents reduce modulo — or modulo the order of the base — never modulo , and only when the base is coprime to . is not .
- Applying the CRT without checking coprimality. The clean statement needs pairwise coprime moduli. Otherwise use the corollary General systems: the system is solvable exactly when for every pair, and checking only consecutive pairs is not enough.
- Expecting two square roots modulo a composite. has eight solutions, one for each choice of sign in each of the three CRT coordinates. Only modulo a prime is the count or .
- Reading a Jacobi symbol as a residue test. for composite does not make a square mod — two hidden non-residues can cancel, as with . Only the value is conclusive.
- Forgetting that a symbol depends on the numerator modulo , not on its size. Always reduce first; is a symbol about , and signed residues often shorten the work further.
- Confusing with . The first is a number, the least nonnegative remainder; the second is a relation between two numbers. Writing " " between an integer and a remainder computation is harmless, but writing " " where the classes only agree is not.