Modular Arithmetic
Modular Arithmetic, the Euclidean Algorithm, and the Chinese Remainder Theorem
Modular arithmetic and congruence
Modular arithmetic studies relationships between integers by keeping track of their remainders after division by a fixed positive integer, called the modulus.
For example, ordinary arithmetic about even and odd numbers is arithmetic modulo 2:
An even number has remainder 0 when divided by 2.
An odd number has remainder 1 when divided by 2.
Adding an even number and an odd number gives an odd number because the remainders add to 0+1≡1(mod2).
Adding two even numbers gives an even number because 0+0≡0(mod2).
The specific values of the integers are not always important; their remainders may be enough to determine the result of an operation.
Congruence
For integers a and b and a positive integer n, the notation
means that a and b differ by a multiple of n. Equivalently, n divides their difference:
For example, 12≡5(mod7) because 12−5=7, a multiple of 7.
Congruence modulo n groups integers into classes that have the same remainder when divided by n. Modulo 7, for instance, 5, 12, and 19 all belong to the same class because each leaves remainder 5.
The division algorithm and remainders
The division algorithm says that for any integer a and any positive integer n, there is a unique pair of integers q and r such that
Here, q is the quotient and r is the remainder. The condition on the remainder means that r must be one of 0,1,…,n−1.
Congruence is equivalent to having the same remainder
Two integers are congruent modulo n if and only if they have the same remainder when divided by n.
To see one direction, suppose
for the same remainder r. Subtracting gives
so a−b is a multiple of n and therefore a≡b(modn).
For the other direction, write the division algorithm for both integers:
where 0≤r,r′<n. If a≡b(modn), then their difference is a multiple of n:
Since the first term is a multiple of n, r−r′ must also be a multiple of n. But the remainder bounds imply
The only multiple of n strictly between −n and n is 0. Thus r−r′=0, so r=r′.
Key interpretation: Congruence modulo n is exactly the statement that two integers leave the same remainder when divided by n.
Clock arithmetic and calculations modulo n
Modular arithmetic is often visualized as clock arithmetic. Imagine placing the remainders 0,1,…,n−1 around a clock. Moving forward by n steps returns to the same position. Thus, on a clock with 7 positions, 12 and 5 land at the same position because 12≡5(mod7).
Addition and multiplication
To add or multiply modulo n, ordinary addition and multiplication can be performed first, after which the result is reduced to its remainder modulo n.
For example,
because 120 leaves remainder 1 when divided by 7. Equivalently, since 10≡3(mod7) and 12≡5(mod7), the product has the same remainder as 3⋅5=15, and 15≡1(mod7).
The reason this works is that replacing an integer by another integer with the same remainder does not change the remainder of a sum or product. In particular, if a≡b(modn) and c≡d(modn), then
and
After calculating, a result is often written as its least nonnegative remainder, which lies between 0 and n−1.
Familiar arithmetic laws
Many familiar laws remain valid in modular arithmetic:
Associativity: Regrouping an addition or multiplication does not change the resulting congruence class.
Commutativity: The order of terms in addition or multiplication does not change the result.
Distributivity: Multiplication distributes over addition, as in a(b+c)≡ab+ac(modn).
The arithmetic can therefore be viewed as ordinary addition and multiplication on the possible remainders. For example, modulo 7, adding 4 to 4 gives 8, whose remainder is 1; hence 4+4≡1(mod7).
The set of remainders modulo n, together with these operations, can be studied as an algebraic structure. In particular, the additive structure modulo n is an example of a group, a concept used to describe sets with operations and consistent algebraic rules.
Modular arithmetic as a tool for privacy and cryptography
Modular arithmetic is important in cryptography, where it is used to construct and analyze methods for encoding information. Multiplication modulo a number can scramble values, while a suitable multiplicative inverse can undo that operation.
A related idea is a zero-knowledge proof: a method for establishing that a claim is true without revealing more information than necessary. A simplified card demonstration illustrates the goal of privately computing whether two people both answer “yes.”
Each participant holds one red card and one black card, and the arrangement is designed so that a “yes” places a red card nearest a central red card, while a “no” places a black card nearest it. After the cards are combined and the sequence is randomly shifted, the resulting pattern can indicate whether both participants answered “yes” without directly displaying either private answer.
This is an illustration of the intuition behind private computation and plausible deniability, not a complete account of a secure cryptographic protocol. Real protocols require more careful definitions and security guarantees.
Multiplicative inverses and the greatest common divisor
A number x is a multiplicative inverse of a modulo n if
For example, modulo 7,
Thus 2 and 4 are multiplicative inverses of one another modulo 7. Multiplying by an inverse can undo multiplication by the original number, which is one reason inverses are useful in cryptographic methods.
The central question is: When does an integer have a multiplicative inverse modulo n? The answer depends on the greatest common divisor.
Greatest common divisor
The greatest common divisor, written gcd(a,b), is the largest positive integer that divides both a and b. If a and b have no common positive divisor other than 1, they are relatively prime, and their greatest common divisor is 1.
For example, gcd(4,7)=1, while gcd(6,9)=3.
A key GCD reduction
Suppose b≥a>0. Then
If an integer divides both a and b, it also divides their difference b−a. Conversely, if an integer divides both a and b−a, it divides their sum a+(b−a)=b. Thus the sets of common divisors are the same.
A stronger version uses the division algorithm. If
then
This follows by repeatedly applying the subtraction property: subtracting a from b a total of q times leaves the remainder r.
The Euclidean algorithm
The Euclidean algorithm repeatedly replaces a pair of integers with the smaller integer and the remainder from dividing the larger by the smaller. Because the GCD does not change at each step, the final nonzero remainder is the GCD.
For example:
so gcd(46,360)=gcd(46,38). Continuing,
The last nonzero remainder is 2, so
This procedure is far more efficient than checking every possible divisor, especially when the integers are large.
The extended Euclidean algorithm and Bézout’s identity
The extended Euclidean algorithm keeps track not only of the remainders but also of how each remainder is built from the original integers. It produces integers s and t satisfying
This equation is called Bézout’s identity.
For example, when computing the GCD of 46 and 360, each intermediate remainder can be written as an integer combination of 46 and 360. Continuing that bookkeeping expresses the final GCD, 2, in the form 46s+360t for some integers s and t.
Bézout’s identity also certifies that the computed GCD is greatest. If d=gcd(a,b)=sa+tb, then every common divisor of a and b divides the combination sa+tb=d. No common divisor can therefore be larger than d.
Criterion for a multiplicative inverse
An integer a has a multiplicative inverse modulo n if and only if
If gcd(a,n)=1, Bézout’s identity gives integers s and t such that
Reducing modulo n removes the multiple nt, leaving
Thus s is an inverse of a modulo n.
Conversely, if as≡1(modn), then for some integer t,
Any common divisor of a and n must divide the left-hand side and hence must divide 1. Therefore gcd(a,n)=1.
For a prime modulus p, every nonzero remainder has an inverse: no nonzero integer smaller than p shares a factor with p. A prime modulus is sufficient for every nonzero residue to have an inverse, but it is not necessary. For instance, 5 has an inverse modulo 6 because gcd(5,6)=1, even though 6 is not prime. The extended Euclidean algorithm finds the inverse by calculating the coefficient s in Bézout’s identity.
The Chinese remainder theorem
The Chinese remainder theorem describes when a number can be recovered from its remainders with respect to different moduli.
Suppose a and b are positive and relatively prime:
For any specified remainders r modulo a and s modulo b, there is exactly one integer n in the range 0≤n<ab such that
In other words, when the moduli are relatively prime, the pair of remainders uniquely identifies a number in that range.
Why the solution is unique
Suppose two integers x and y in the range 0≤x,y<ab have the same remainders modulo both a and b. Then
Since a and b are relatively prime, their product divides the difference:
But two distinct integers between 0 and ab−1 cannot differ by a nonzero multiple of ab. Therefore x=y. This proves that two different integers in the range cannot have the same pair of remainders.
There are ab possible integers in the range and ab possible pairs of remainders: a choices modulo a and b choices modulo b. Since no two integers produce the same pair, every possible pair occurs. Thus a solution exists for every pair of specified remainders, and it is unique.
Constructing the solution
The solution can also be computed directly. Write
because s is the remainder of n modulo b. The other condition requires
so
Because gcd(a,b)=1, b has a multiplicative inverse modulo a. Multiplying by that inverse gives
Choosing the representative k with 0≤k<a produces n=kb+s in the range 0≤n<ab.
For example, consider
Set n=7k+1. The first condition gives
Since 7≡2(mod5) and the inverse of 2 modulo 5 is 3, it follows that k≡4(mod5). Taking k=4 gives
Indeed, 29 leaves remainder 4 when divided by 5 and remainder 1 when divided by 7.
Applications: reconstructing information from remainders
A simplified illustration of fountain codes treats a large file as an integer N. A sender broadcasts pieces of information consisting of the remainders of N modulo various relatively prime moduli.
A receiver collects pieces such as
If the moduli are pairwise relatively prime and their product is greater than N, the Chinese remainder theorem allows the receiver to reconstruct the unique integer represented by those remainders. Different receivers can collect different sets of pieces; each can recover the file once the combined moduli provide enough information.
This illustrates how modular arithmetic can support distributing information to many recipients. Practical fountain codes are more sophisticated: they account for issues such as missing or corrupted messages, rather than simply transmitting integer remainders.

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