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

Foundations: Divisibility and Bézout’s Identity

Two facts about divisibility provide useful tools for modular arithmetic.

  • Bézout’s identity: For any integers aaa and bbb, there exist integers sss and ttt such that

    gcd⁡(a,b)=as+bt.\gcd(a,b)=as+bt.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 aaa and bbb if and only if it is divisible by gcd⁡(a,b)\gcd(a,b)gcd(a,b). In particular, 111 is such a combination exactly when gcd⁡(a,b)=1\gcd(a,b)=1gcd(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 nnn means treating numbers that differ by a multiple of nnn as equivalent.

This idea appears in familiar settings:

  • Even and odd numbers: These are remainders modulo 222. An even number plus an odd number is always odd.

  • Last digits: The last digit of an integer is its remainder modulo 101010. To find the last digit of a product, it is enough to multiply the factors’ remainders modulo 101010.

  • Time on a clock: Clock arithmetic uses a modulus such as 121212 or 242424 for hours.

  • Days of the week: The weekday repeats every 777 days, so dates separated by a multiple of 777 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 aaa and bbb are congruent modulo nnn if their difference is divisible by nnn:

a≡b(modn)⟺n∣(a−b).a\equiv b\pmod n \quad\Longleftrightarrow\quad n\mid(a-b).a≡b(modn)⟺n∣(a−b).

Congruence means that aaa and bbb have the same remainder when divided by nnn.

For example, modulo 555:

  • 17≡12(mod5)17\equiv12\pmod 517≡12(mod5), since 17−12=517-12=517−12=5.

  • 17≡32(mod5)17\equiv32\pmod 517≡32(mod5), since 17−32=−1517-32=-1517−32=−15.

  • 17≢33(mod5)17\not\equiv33\pmod 517≡33(mod5), since 17−33=−1617-33=-1617−33=−16 is not divisible by 555.

Congruence modulo nnn groups all integers into nnn residue classes, represented by 0,1,…,n−10,1,\ldots,n-10,1,…,n−1. For instance, modulo 555, the class represented by 111 contains all integers congruent to 111, including 1,6,11,−4,−9,…1,6,11,-4,-9,\ldots1,6,11,−4,−9,…. Every integer belongs to the class represented by its remainder.


Division Theorem and Remainders

For every integer NNN and every positive integer ddd, there is a unique pair of integers qqq and rrr such that

N=qd+rwith0≤r<d.N=qd+r \qquad\text{with}\qquad 0\le r<d.N=qd+rwith0≤r<d.

Here, qqq is the quotient and rrr is the remainder. The condition 0≤r<d0\le r<d0≤r<d means that even when NNN is negative, the remainder is chosen to be nonnegative.

Congruence and equality of remainders

For integers aaa and bbb,

a≡b(modn)⟺rem⁡(a,n)=rem⁡(b,n).a\equiv b\pmod n \quad\Longleftrightarrow\quad \operatorname{rem}(a,n)=\operatorname{rem}(b,n).a≡b(modn)⟺rem(a,n)=rem(b,n).

Both directions follow from the division theorem:

  • If the remainders match, write a=qn+ra=qn+ra=qn+r and b=q′n+rb=q'n+rb=q′n+r. Then a−b=(q−q′)na-b=(q-q')na−b=(q−q′)n, so nnn divides the difference.

  • If a≡b(modn)a\equiv b\pmod na≡b(modn), then a=b+kna=b+kna=b+kn for some integer kkk. Adding a multiple of nnn does not change a number’s remainder modulo nnn, so aaa and bbb have the same remainder.

Thus, when working modulo nnn, every integer can be replaced by its remainder in {0,1,…,n−1}\{0,1,\ldots,n-1\}{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 nnn always satisfies 0≤r<n0\le r<n0≤r<n.

It is also useful to distinguish a congruence relation from a remainder operation:

  • a≡b(modn)a\equiv b\pmod na≡b(modn) says that aaa and bbb belong to the same residue class.

  • rem⁡(a,n)\operatorname{rem}(a,n)rem(a,n) is the particular remainder of aaa upon division by nnn.

These are different kinds of statements: one compares two integers; the other produces a single integer.


Arithmetic with Congruences

If a≡b(modn)a\equiv b\pmod na≡b(modn), then adding, subtracting, or multiplying by the same integer preserves congruence. For every integer ccc,

a+c≡b+c(modn),a+c\equiv b+c\pmod n,a+c≡b+c(modn),
a−c≡b−c(modn),a-c\equiv b-c\pmod n,a−c≡b−c(modn),
c−a≡c−b(modn),c-a\equiv c-b\pmod n,c−a≡c−b(modn),

and

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

Consequently, during a calculation modulo nnn, 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)a\equiv3\pmod5a≡3(mod5) and b≡4(mod5)b\equiv4\pmod5b≡4(mod5), then

a+b≡3+4≡7≡2(mod5).a+b\equiv3+4\equiv7\equiv2\pmod5.a+b≡3+4≡7≡2(mod5).

The result follows by adding the representatives 333 and 444 and then reducing the sum modulo 555.

Exponentiation and the base

Congruent bases remain congruent when raised to the same positive integer power:

a≡b(modn)⟹ac≡bc(modn)a\equiv b\pmod n \quad\Longrightarrow\quad a^c\equiv b^c\pmod na≡b(modn)⟹ac≡bc(modn)

for positive integers ccc. This follows by repeated multiplication: each factor of aaa can be replaced by the congruent factor bbb.

However, congruence does not generally allow the exponent itself to be replaced by a congruent exponent. For example, −1≡4(mod5)-1\equiv4\pmod5−1≡4(mod5), but

(−1)1=−1≡4(mod5),(−1)6=1(mod5).(-1)^1=-1\equiv4\pmod5, \qquad (-1)^6=1\pmod5.(−1)1=−1≡4(mod5),(−1)6=1(mod5).

Thus (−1)1≢(−1)6(mod5)(-1)^1\not\equiv(-1)^6\pmod5(−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 nnn as early and as often as possible.

For example, to work modulo 100100100:

11335≡35(mod100),7799≡−1(mod100).11335\equiv35\pmod{100}, \qquad 7799\equiv-1\pmod{100}.11335≡35(mod100),7799≡−1(mod100).

Suppose the expression is grouped as

x=1133511111(6+77995000).x=11335^{11111}\bigl(6+7799^{5000}\bigr).x=1133511111(6+77995000).

The congruent replacements give

x≡3511111(6+(−1)5000)(mod100).x\equiv35^{11111}\bigl(6+(-1)^{5000}\bigr)\pmod{100}.x≡3511111(6+(−1)5000)(mod100).

Because 500050005000 is even, (−1)5000=1(-1)^{5000}=1(−1)5000=1, so the factor in parentheses is 777.

To evaluate the remaining power, the early powers of 353535 modulo 100100100 are

352≡25,353≡75,354≡25(mod100).35^2\equiv25,\qquad 35^3\equiv75,\qquad 35^4\equiv25\pmod{100}.352≡25,353≡75,354≡25(mod100).

The powers from the second onward alternate between 252525 for even exponents and 757575 for odd exponents. Since 111111111111111 is odd and greater than 111,

3511111≡75(mod100).35^{11111}\equiv75\pmod{100}.3511111≡75(mod100).

Therefore,

x≡75⋅7=525≡25(mod100).x\equiv75\cdot7=525\equiv25\pmod{100}.x≡75⋅7=525≡25(mod100).

The method avoids calculating enormous integer powers. It reduces the bases and intermediate results modulo 100100100 and uses the repeating pattern of the powers.

Why powers eventually repeat

Modulo nnn, there are only nnn 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,

3x≡3(mod6)3x\equiv3\pmod63x≡3(mod6)

does not imply x≡1(mod6)x\equiv1\pmod6x≡1(mod6): x=3x=3x=3 also satisfies the original congruence, because 3⋅3=9≡3(mod6)3\cdot3=9\equiv3\pmod63⋅3=9≡3(mod6). In fact, the solutions are the odd residue classes modulo 666, not just the class represented by 111.

The relevant concept is a multiplicative inverse.

Definition and examples

A number bbb is a multiplicative inverse of aaa modulo nnn if

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

If such an inverse exists, multiplying a congruence by it can cancel the factor aaa.

Examples:

  • The inverse of 111 modulo any positive nnn is 111.

  • Modulo 555, 222 and 333 are inverses of each other because 2⋅3=6≡1(mod5)2\cdot3=6\equiv1\pmod52⋅3=6≡1(mod5).

When an inverse exists

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.

To see why, an inverse is a number bbb satisfying ab≡1(modn)ab\equiv1\pmod nab≡1(modn). By the definition of congruence, this means that nnn divides ab−1ab-1ab−1. Thus, for some integer qqq,

ab−1=qn,ab-1=qn,ab−1=qn,

or equivalently,

1=ab−qn.1=ab-qn.1=ab−qn.

This expresses 111 as an integer linear combination of aaa and nnn. By Bézout’s identity, such a combination exists exactly when gcd⁡(a,n)=1\gcd(a,n)=1gcd(a,n)=1.

This criterion explains why the factor 333 cannot be canceled modulo 666: gcd⁡(3,6)=3\gcd(3,6)=3gcd(3,6)=3, so 333 has no inverse modulo 666. In contrast, gcd⁡(3,5)=1\gcd(3,5)=1gcd(3,5)=1, so 333 can be canceled modulo 555.

For example, the inverse of 777 modulo 303030 is 131313, since 7⋅13=91≡1(mod30)7\cdot13=91\equiv1\pmod{30}7⋅13=91≡1(mod30). Therefore,

7x≡14(mod30)7x\equiv14\pmod{30}7x≡14(mod30)

can be multiplied by 131313 on both sides to give

x≡13⋅14=182≡2(mod30).x\equiv13\cdot14=182\equiv2\pmod{30}.x≡13⋅14=182≡2(mod30).

Fermat’s Little Theorem

Fermat’s Little Theorem describes a special way to simplify exponents when the modulus is prime.

If ppp is prime and aaa is not divisible by ppp, then

ap−1≡1(modp).a^{p-1}\equiv1\pmod p.ap−1≡1(modp).

The condition that aaa is not divisible by ppp is essential. If p∣ap\mid ap∣a, then ap−1≡0(modp)a^{p-1}\equiv0\pmod pap−1≡0(modp), not 111.

Proof idea

Consider the numbers

a,2a,3a,…,(p−1)aa,2a,3a,\ldots,(p-1)aa,2a,3a,…,(p−1)a

modulo ppp. Since p∤ap\nmid ap∤a and ppp is prime, gcd⁡(a,p)=1\gcd(a,p)=1gcd(a,p)=1, so aaa has an inverse modulo ppp. Multiplication by aaa therefore sends the nonzero residues modulo ppp to nonzero residues without making any two of them equal. The displayed numbers, reduced modulo ppp, are exactly the nonzero residues in some order.

Multiplying all these congruences together gives

ap−1(p−1)!≡(p−1)!(modp).a^{p-1}(p-1)!\equiv(p-1)!\pmod p.ap−1(p−1)!≡(p−1)!(modp).

None of 1,2,…,p−11,2,\ldots,p-11,2,…,p−1 is divisible by ppp, so (p−1)!(p-1)!(p−1)! is not divisible by ppp and has an inverse modulo ppp. It can therefore be canceled, yielding

ap−1≡1(modp).a^{p-1}\equiv1\pmod p.ap−1≡1(modp).

Reducing exponents using the theorem

For prime modulus ppp and a base aaa not divisible by ppp, a factor of ap−1a^{p-1}ap−1 can be removed from a power without changing its value modulo ppp. This makes exponents reducible modulo p−1p-1p−1.

For example, to calculate 795(mod13)7^{95}\pmod{13}795(mod13), Fermat’s Little Theorem applies because 131313 is prime and 13∤713\nmid713∤7. Since 95=12⋅7+1195=12\cdot7+1195=12⋅7+11,

795≡711(mod13).7^{95}\equiv7^{11}\pmod{13}.795≡711(mod13).

Also, 712≡1(mod13)7^{12}\equiv1\pmod{13}712≡1(mod13), so 7117^{11}711 is the inverse of 777 modulo 131313. Since 7⋅2=14≡1(mod13)7\cdot2=14\equiv1\pmod{13}7⋅2=14≡1(mod13), that inverse is 222, and hence

795≡2(mod13).7^{95}\equiv2\pmod{13}.795≡2(mod13).

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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent