Modular Arithmetic
Modular Arithmetic
Foundations: Divisibility and Bézout’s Identity
Two facts about divisibility provide useful tools for modular arithmetic.
Bézout’s identity: For any integers a and b, there exist integers s and t such that
gcd(a,b)=as+bt.The extended Euclidean algorithm, also called the pulverizer, can find such coefficients efficiently.
Integer linear combinations: An integer can be written as an integer linear combination of a and b if and only if it is divisible by gcd(a,b). In particular, 1 is such a combination exactly when gcd(a,b)=1.
The second fact will be important when deciding whether a number can be divided out in a modular congruence.
What Modular Arithmetic Means
Modular arithmetic compares integers by their remainders after division by a fixed positive integer, called the modulus. Working modulo n means treating numbers that differ by a multiple of n as equivalent.
This idea appears in familiar settings:
Even and odd numbers: These are remainders modulo 2. An even number plus an odd number is always odd.
Last digits: The last digit of an integer is its remainder modulo 10. To find the last digit of a product, it is enough to multiply the factors’ remainders modulo 10.
Time on a clock: Clock arithmetic uses a modulus such as 12 or 24 for hours.
Days of the week: The weekday repeats every 7 days, so dates separated by a multiple of 7 days have the same weekday.
In each case, full multiples of the modulus can be ignored because they do not affect the remainder.
Congruence
Integers a and b are congruent modulo n if their difference is divisible by n:
Congruence means that a and b have the same remainder when divided by n.
For example, modulo 5:
17≡12(mod5), since 17−12=5.
17≡32(mod5), since 17−32=−15.
17≡33(mod5), since 17−33=−16 is not divisible by 5.
Congruence modulo n groups all integers into n residue classes, represented by 0,1,…,n−1. For instance, modulo 5, the class represented by 1 contains all integers congruent to 1, including 1,6,11,−4,−9,…. Every integer belongs to the class represented by its remainder.
Division Theorem and Remainders
For every integer N and every positive integer d, there is a unique pair of integers q and r such that
Here, q is the quotient and r is the remainder. The condition 0≤r<d means that even when N is negative, the remainder is chosen to be nonnegative.
Congruence and equality of remainders
For integers a and b,
Both directions follow from the division theorem:
If the remainders match, write a=qn+r and b=q′n+r. Then a−b=(q−q′)n, so n divides the difference.
If a≡b(modn), then a=b+kn for some integer k. Adding a multiple of n does not change a number’s remainder modulo n, so a and b have the same remainder.
Thus, when working modulo n, every integer can be replaced by its remainder in {0,1,…,n−1}.
Notation caution
Some programming languages use an operation called “mod” or “remainder.” Its treatment of negative inputs can vary. In these notes, a remainder modulo a positive n always satisfies 0≤r<n.
It is also useful to distinguish a congruence relation from a remainder operation:
a≡b(modn) says that a and b belong to the same residue class.
rem(a,n) is the particular remainder of a upon division by n.
These are different kinds of statements: one compares two integers; the other produces a single integer.
Arithmetic with Congruences
If a≡b(modn), then adding, subtracting, or multiplying by the same integer preserves congruence. For every integer c,
and
Consequently, during a calculation modulo n, an integer can be replaced by any congruent integer. Replacements can be made at intermediate steps, not only at the end.
For example, if a≡3(mod5) and b≡4(mod5), then
The result follows by adding the representatives 3 and 4 and then reducing the sum modulo 5.
Exponentiation and the base
Congruent bases remain congruent when raised to the same positive integer power:
for positive integers c. This follows by repeated multiplication: each factor of a can be replaced by the congruent factor b.
However, congruence does not generally allow the exponent itself to be replaced by a congruent exponent. For example, −1≡4(mod5), but
Thus (−1)1≡(−1)6(mod5). A separate theorem is needed to justify reducing exponents in particular circumstances.
Efficient Modular Computation
Large calculations can often be simplified by reducing numbers modulo n as early and as often as possible.
For example, to work modulo 100:
Suppose the expression is grouped as
The congruent replacements give
Because 5000 is even, (−1)5000=1, so the factor in parentheses is 7.
To evaluate the remaining power, the early powers of 35 modulo 100 are
The powers from the second onward alternate between 25 for even exponents and 75 for odd exponents. Since 11111 is odd and greater than 1,
Therefore,
The method avoids calculating enormous integer powers. It reduces the bases and intermediate results modulo 100 and uses the repeating pattern of the powers.
Why powers eventually repeat
Modulo n, there are only n possible remainders. Therefore, among sufficiently many powers of a fixed integer, two must have the same remainder. Once a power repeats, subsequent powers repeat as well, because each next power is obtained by multiplying the previous one by the same base. The sequence of remainders is therefore eventually periodic, though the repeating pattern may take more work to find than in the example above.
Division and Multiplicative Inverses
Division is not always permitted in modular arithmetic. For example,
does not imply x≡1(mod6): x=3 also satisfies the original congruence, because 3⋅3=9≡3(mod6). In fact, the solutions are the odd residue classes modulo 6, not just the class represented by 1.
The relevant concept is a multiplicative inverse.
Definition and examples
A number b is a multiplicative inverse of a modulo n if
If such an inverse exists, multiplying a congruence by it can cancel the factor a.
Examples:
The inverse of 1 modulo any positive n is 1.
Modulo 5, 2 and 3 are inverses of each other because 2⋅3=6≡1(mod5).
When an inverse exists
An integer a has a multiplicative inverse modulo n if and only if
To see why, an inverse is a number b satisfying ab≡1(modn). By the definition of congruence, this means that n divides ab−1. Thus, for some integer q,
or equivalently,
This expresses 1 as an integer linear combination of a and n. By Bézout’s identity, such a combination exists exactly when gcd(a,n)=1.
This criterion explains why the factor 3 cannot be canceled modulo 6: gcd(3,6)=3, so 3 has no inverse modulo 6. In contrast, gcd(3,5)=1, so 3 can be canceled modulo 5.
For example, the inverse of 7 modulo 30 is 13, since 7⋅13=91≡1(mod30). Therefore,
can be multiplied by 13 on both sides to give
Fermat’s Little Theorem
Fermat’s Little Theorem describes a special way to simplify exponents when the modulus is prime.
If p is prime and a is not divisible by p, then
The condition that a is not divisible by p is essential. If p∣a, then ap−1≡0(modp), not 1.
Proof idea
Consider the numbers
modulo p. Since p∤a and p is prime, gcd(a,p)=1, so a has an inverse modulo p. Multiplication by a therefore sends the nonzero residues modulo p to nonzero residues without making any two of them equal. The displayed numbers, reduced modulo p, are exactly the nonzero residues in some order.
Multiplying all these congruences together gives
None of 1,2,…,p−1 is divisible by p, so (p−1)! is not divisible by p and has an inverse modulo p. It can therefore be canceled, yielding
Reducing exponents using the theorem
For prime modulus p and a base a not divisible by p, a factor of ap−1 can be removed from a power without changing its value modulo p. This makes exponents reducible modulo p−1.
For example, to calculate 795(mod13), Fermat’s Little Theorem applies because 13 is prime and 13∤7. Since 95=12⋅7+11,
Also, 712≡1(mod13), so 711 is the inverse of 7 modulo 13. Since 7⋅2=14≡1(mod13), that inverse is 2, and hence
Fermat’s Little Theorem provides a valid way to reduce exponents under its stated conditions; exponents cannot be reduced modulo the modulus in general.

Ask anything
We've read this whole study set, so questions about it get the best answers.