1. Home
  2. Explore
  3. Mathematics
  4. Modular Arithmetic

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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 222:

  • An even number has remainder 000 when divided by 222.

  • An odd number has remainder 111 when divided by 222.

  • Adding an even number and an odd number gives an odd number because the remainders add to 0+1≡1(mod2)0+1\equiv 1\pmod 20+1≡1(mod2).

  • Adding two even numbers gives an even number because 0+0≡0(mod2)0+0\equiv 0\pmod 20+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 aaa and bbb and a positive integer nnn, the notation

a≡b(modn)a\equiv b\pmod na≡b(modn)

means that aaa and bbb differ by a multiple of nnn. Equivalently, nnn divides their difference:

n∣(a−b).n\mid(a-b).n∣(a−b).

For example, 12≡5(mod7)12\equiv 5\pmod 712≡5(mod7) because 12−5=712-5=712−5=7, a multiple of 777.

Congruence modulo nnn groups integers into classes that have the same remainder when divided by nnn. Modulo 777, for instance, 555, 121212, and 191919 all belong to the same class because each leaves remainder 555.


The division algorithm and remainders

The division algorithm says that for any integer aaa and any positive integer nnn, there is a unique pair of integers qqq and rrr such that

a=qn+r,0≤r<n.a=qn+r,\qquad 0\le r<n.a=qn+r,0≤r<n.

Here, qqq is the quotient and rrr is the remainder. The condition on the remainder means that rrr must be one of 0,1,…,n−10,1,\ldots,n-10,1,…,n−1.

Congruence is equivalent to having the same remainder

Two integers are congruent modulo nnn if and only if they have the same remainder when divided by nnn.

To see one direction, suppose

a=qn+r,b=q′n+ra=qn+r,\qquad b=q'n+ra=qn+r,b=q′n+r

for the same remainder rrr. Subtracting gives

a−b=(q−q′)n,a-b=(q-q')n,a−b=(q−q′)n,

so a−ba-ba−b is a multiple of nnn and therefore a≡b(modn)a\equiv b\pmod na≡b(modn).

For the other direction, write the division algorithm for both integers:

a=qn+r,b=q′n+r′,a=qn+r,\qquad b=q'n+r',a=qn+r,b=q′n+r′,

where 0≤r,r′<n0\le r,r'<n0≤r,r′<n. If a≡b(modn)a\equiv b\pmod na≡b(modn), then their difference is a multiple of nnn:

a−b=(q−q′)n+(r−r′).a-b=(q-q')n+(r-r').a−b=(q−q′)n+(r−r′).

Since the first term is a multiple of nnn, r−r′r-r'r−r′ must also be a multiple of nnn. But the remainder bounds imply

−n<r−r′<n.-n<r-r'<n.−n<r−r′<n.

The only multiple of nnn strictly between −n-n−n and nnn is 000. Thus r−r′=0r-r'=0r−r′=0, so r=r′r=r'r=r′.

Key interpretation: Congruence modulo nnn is exactly the statement that two integers leave the same remainder when divided by nnn.


Clock arithmetic and calculations modulo nnn

Modular arithmetic is often visualized as clock arithmetic. Imagine placing the remainders 0,1,…,n−10,1,\ldots,n-10,1,…,n−1 around a clock. Moving forward by nnn steps returns to the same position. Thus, on a clock with 777 positions, 121212 and 555 land at the same position because 12≡5(mod7)12\equiv 5\pmod 712≡5(mod7).

Addition and multiplication

To add or multiply modulo nnn, ordinary addition and multiplication can be performed first, after which the result is reduced to its remainder modulo nnn.

For example,

10⋅12=120≡1(mod7),10\cdot 12=120\equiv 1\pmod 7,10⋅12=120≡1(mod7),

because 120120120 leaves remainder 111 when divided by 777. Equivalently, since 10≡3(mod7)10\equiv 3\pmod 710≡3(mod7) and 12≡5(mod7)12\equiv 5\pmod 712≡5(mod7), the product has the same remainder as 3⋅5=153\cdot5=153⋅5=15, and 15≡1(mod7)15\equiv1\pmod715≡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)a\equiv b\pmod na≡b(modn) and c≡d(modn)c\equiv d\pmod nc≡d(modn), then

a+c≡b+d(modn)a+c\equiv b+d\pmod na+c≡b+d(modn)

and

ac≡bd(modn).ac\equiv bd\pmod n.ac≡bd(modn).

After calculating, a result is often written as its least nonnegative remainder, which lies between 000 and n−1n-1n−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)a(b+c)\equiv ab+ac\pmod na(b+c)≡ab+ac(modn).

The arithmetic can therefore be viewed as ordinary addition and multiplication on the possible remainders. For example, modulo 777, adding 444 to 444 gives 888, whose remainder is 111; hence 4+4≡1(mod7)4+4\equiv1\pmod74+4≡1(mod7).

The set of remainders modulo nnn, together with these operations, can be studied as an algebraic structure. In particular, the additive structure modulo nnn 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 xxx is a multiplicative inverse of aaa modulo nnn if

ax≡1(modn).ax\equiv1\pmod n.ax≡1(modn).

For example, modulo 777,

2⋅4=8≡1(mod7).2\cdot4=8\equiv1\pmod7.2⋅4=8≡1(mod7).

Thus 222 and 444 are multiplicative inverses of one another modulo 777. 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 nnn? The answer depends on the greatest common divisor.

Greatest common divisor

The greatest common divisor, written gcd⁡(a,b)\gcd(a,b)gcd(a,b), is the largest positive integer that divides both aaa and bbb. If aaa and bbb have no common positive divisor other than 111, they are relatively prime, and their greatest common divisor is 111.

For example, gcd⁡(4,7)=1\gcd(4,7)=1gcd(4,7)=1, while gcd⁡(6,9)=3\gcd(6,9)=3gcd(6,9)=3.

A key GCD reduction

Suppose b≥a>0b\ge a>0b≥a>0. Then

gcd⁡(a,b)=gcd⁡(a,b−a).\gcd(a,b)=\gcd(a,b-a).gcd(a,b)=gcd(a,b−a).

If an integer divides both aaa and bbb, it also divides their difference b−ab-ab−a. Conversely, if an integer divides both aaa and b−ab-ab−a, it divides their sum a+(b−a)=ba+(b-a)=ba+(b−a)=b. Thus the sets of common divisors are the same.

A stronger version uses the division algorithm. If

b=qa+r,0≤r<a,b=qa+r,\qquad 0\le r<a,b=qa+r,0≤r<a,

then

gcd⁡(a,b)=gcd⁡(a,r).\gcd(a,b)=\gcd(a,r).gcd(a,b)=gcd(a,r).

This follows by repeatedly applying the subtraction property: subtracting aaa from bbb a total of qqq times leaves the remainder rrr.

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:

360=7⋅46+38,360=7\cdot46+38,360=7⋅46+38,

so gcd⁡(46,360)=gcd⁡(46,38)\gcd(46,360)=\gcd(46,38)gcd(46,360)=gcd(46,38). Continuing,

46=1⋅38+8,46=1\cdot38+8,46=1⋅38+8,
38=4⋅8+6,38=4\cdot8+6,38=4⋅8+6,
8=1⋅6+2,8=1\cdot6+2,8=1⋅6+2,
6=3⋅2+0.6=3\cdot2+0.6=3⋅2+0.

The last nonzero remainder is 222, so

gcd⁡(46,360)=2.\gcd(46,360)=2.gcd(46,360)=2.

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 sss and ttt satisfying

sa+tb=gcd⁡(a,b).sa+tb=\gcd(a,b).sa+tb=gcd(a,b).

This equation is called Bézout’s identity.

For example, when computing the GCD of 464646 and 360360360, each intermediate remainder can be written as an integer combination of 464646 and 360360360. Continuing that bookkeeping expresses the final GCD, 222, in the form 46s+360t46s+360t46s+360t for some integers sss and ttt.

Bézout’s identity also certifies that the computed GCD is greatest. If d=gcd⁡(a,b)=sa+tbd=\gcd(a,b)=sa+tbd=gcd(a,b)=sa+tb, then every common divisor of aaa and bbb divides the combination sa+tb=dsa+tb=dsa+tb=d. No common divisor can therefore be larger than ddd.

Criterion for a multiplicative inverse

An integer aaa has a multiplicative inverse modulo nnn if and only if

gcd⁡(a,n)=1.\gcd(a,n)=1.gcd(a,n)=1.

If gcd⁡(a,n)=1\gcd(a,n)=1gcd(a,n)=1, Bézout’s identity gives integers sss and ttt such that

as+nt=1.as+nt=1.as+nt=1.

Reducing modulo nnn removes the multiple ntntnt, leaving

as≡1(modn).as\equiv1\pmod n.as≡1(modn).

Thus sss is an inverse of aaa modulo nnn.

Conversely, if as≡1(modn)as\equiv1\pmod nas≡1(modn), then for some integer ttt,

as−nt=1.as-nt=1.as−nt=1.

Any common divisor of aaa and nnn must divide the left-hand side and hence must divide 111. Therefore gcd⁡(a,n)=1\gcd(a,n)=1gcd(a,n)=1.

For a prime modulus ppp, every nonzero remainder has an inverse: no nonzero integer smaller than ppp shares a factor with ppp. A prime modulus is sufficient for every nonzero residue to have an inverse, but it is not necessary. For instance, 555 has an inverse modulo 666 because gcd⁡(5,6)=1\gcd(5,6)=1gcd(5,6)=1, even though 666 is not prime. The extended Euclidean algorithm finds the inverse by calculating the coefficient sss 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 aaa and bbb are positive and relatively prime:

gcd⁡(a,b)=1.\gcd(a,b)=1.gcd(a,b)=1.

For any specified remainders rrr modulo aaa and sss modulo bbb, there is exactly one integer nnn in the range 0≤n<ab0\le n<ab0≤n<ab such that

n≡r(moda),n≡s(modb).n\equiv r\pmod a,\qquad n\equiv s\pmod b.n≡r(moda),n≡s(modb).

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 xxx and yyy in the range 0≤x,y<ab0\le x,y<ab0≤x,y<ab have the same remainders modulo both aaa and bbb. Then

a∣(x−y),b∣(x−y).a\mid(x-y),\qquad b\mid(x-y).a∣(x−y),b∣(x−y).

Since aaa and bbb are relatively prime, their product divides the difference:

ab∣(x−y).ab\mid(x-y).ab∣(x−y).

But two distinct integers between 000 and ab−1ab-1ab−1 cannot differ by a nonzero multiple of ababab. Therefore x=yx=yx=y. This proves that two different integers in the range cannot have the same pair of remainders.

There are ababab possible integers in the range and ababab possible pairs of remainders: aaa choices modulo aaa and bbb choices modulo bbb. 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

n=kb+s,n=kb+s,n=kb+s,

because sss is the remainder of nnn modulo bbb. The other condition requires

kb+s≡r(moda),kb+s\equiv r\pmod a,kb+s≡r(moda),

so

kb≡r−s(moda).kb\equiv r-s\pmod a.kb≡r−s(moda).

Because gcd⁡(a,b)=1\gcd(a,b)=1gcd(a,b)=1, bbb has a multiplicative inverse modulo aaa. Multiplying by that inverse gives

k≡b−1(r−s)(moda).k\equiv b^{-1}(r-s)\pmod a.k≡b−1(r−s)(moda).

Choosing the representative kkk with 0≤k<a0\le k<a0≤k<a produces n=kb+sn=kb+sn=kb+s in the range 0≤n<ab0\le n<ab0≤n<ab.

For example, consider

n≡4(mod5),n≡1(mod7).n\equiv4\pmod5,\qquad n\equiv1\pmod7.n≡4(mod5),n≡1(mod7).

Set n=7k+1n=7k+1n=7k+1. The first condition gives

7k≡3(mod5).7k\equiv3\pmod5.7k≡3(mod5).

Since 7≡2(mod5)7\equiv2\pmod57≡2(mod5) and the inverse of 222 modulo 555 is 333, it follows that k≡4(mod5)k\equiv4\pmod5k≡4(mod5). Taking k=4k=4k=4 gives

n=7⋅4+1=29.n=7\cdot4+1=29.n=7⋅4+1=29.

Indeed, 292929 leaves remainder 444 when divided by 555 and remainder 111 when divided by 777.


Applications: reconstructing information from remainders

A simplified illustration of fountain codes treats a large file as an integer NNN. A sender broadcasts pieces of information consisting of the remainders of NNN modulo various relatively prime moduli.

A receiver collects pieces such as

N mod a1,N mod a2,…N\bmod a_1,\quad N\bmod a_2,\quad \ldotsNmoda1​,Nmoda2​,…

If the moduli are pairwise relatively prime and their product is greater than NNN, 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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent