8.2 Modular Arithmetic: A Case Study

Let us consider a context in which different integers can be seen to be the “same".

Consider an analogue clock.

321121110987654
Figure 8.1: Analogue clock showing 1:00 (or 13:00)

At 1 o’clock, a digital clock may read either 1:00 or 13:00 while from an analogue perspective, these are the same time. Alternatively, if you are scheduling something in 24 hours time, then that is equivalent to scheduling something at the same time the next day.

We can formalise and generalise these ideas. Recall that any integer aa can be written as a=qd+ra=qd+r where dd is some integer divisor and rr is the remainder: an integer such that 0r<d0\leq r<d.

For example 25=3×7+425=3\times 7+4. Hence 44 is the remainder when 2525 is divided by 77. The quotient, qq, is 33.

At first glance this appears silly: why not just write 257=3+47\frac{25}{7}=3+\frac{4}{7}? The reason is that by writing 25=3×7+425=3\times 7+4 all terms remain integers. That is, we continue to work exclusively within the integer number system.

Note that the remainder behaves like our clock: it increases as expected until we get to an upper bound, in this case dd, when it then ’resets’ and starts counting up again from 0. If we only want to consider two integers as being “different” if they have different remainders (e.g. if we only wanted to keep track of the time in the day and didn’t care which day it was), we could use the language of congruence.

Definition 8.2 (Congruence).

For mm\in\mathbb{N} and a,ba,b\in\mathbb{Z}, we say aa is congruent to bb modulo mm, and write a=bmodma=b\mod m, or sometimes abmodma\equiv b\mod m, if there is some qq\in\mathbb{Z} such that ba=qmb-a=qm.

Example 8.3.
  1. (a)

    133mod1013\equiv 3\mod 10 as 133=1013-3=10 is divisible by 10.

  2. (b)

    As we saw with the clock, 131mod1213\equiv 1\mod 12 as 131=1213-1=12.

  3. (c)

    712mod2371\equiv 2\mod 23 as 712=69=3×2371-2=69=3\times 23.

Note that when we work with congruent integers, we always clearly state what value mm we are working with. This is very important as changing the value mm completely changes which integers are equivalent. In particular, every integer is congruent to 0mod10\mod 1.

There are a number of ways we can view congruent integers.

Theorem 8.4.

Let mm\in\mathbb{N} and a,ba,b\in\mathbb{Z}

The following are equivalent:

  1. (a)

    a=bmodma=b\mod m;

  2. (b)

    mm divides aba-b;

  3. (c)

    aa and bb have same remainder when divided by mm;

  4. (d)

    b=qm+ab=qm+a for some qq\in\mathbb{Z}.

Proof.

(a) \implies (d) and (d) \implies (a) both follow directly from Definition 8.2.

For (d) \implies (b), if ba=qmb-a=qm for some qq\in\mathbb{Z}, then mm is a divisor of bab-a by definition.

For (b) \implies (c), we know there exist integers pp and qq such that a=pm+ra=pm+r and b=qm+sb=qm+s where rr and ss are remainders. Then ab=(pq)m+(rs)a-b=(p-q)m+(r-s). If mm divides aba-b, then rs=0r-s=0 so r=sr=s as required.

Finally, (c)(c) \implies (d) as if aa and bb have the same remainder rr when divided by mm, then a=pm+ra=pm+r and b=m+rb=\ell m+r for some integers p,p,\ell, so, substituting for rr, we can write b=m+(apm)=(p)m+ab=\ell m+(a-pm)=(\ell-p)m+a. As \ell and pp are both integers, we have written b=qm+ab=qm+a where q=pq=\ell-p is an integer.

We have therefore shown that all of these statements are equivalent to one another.

Corollary 8.5.

Every integer is congruent to exactly one of the numbers 0,1,2,,m1modm0,1,2,\cdots,m-1\mod m.

Proof.

Let nn\in\mathbb{Z}. Then we can uniquely write n=qm+rn=qm+r where rr is the remainder with 0rm10\leq r\leq m-1.

Then nr=qmn-r=qm so, by Definition 8.2, n=rmodmn=r\mod m which concludes the proof.

Exercise 8.6.

Working mod11\mod 11, write 5858 in three different ways.

Solution (please try for yourself before looking)

Since 58=(5×11)+3=(6×11)8=(4×11)+1458=(5\times 11)+3=(6\times 11)-8=(4\times 11)+14, we have

58=3=8=14mod11.58=3=-8=14\mod 11.
Corollary 8.7.

Let pp\in\mathbb{N} be a divisor of mm\in\mathbb{N}, p1p\neq 1, and a=bmodma=b\mod m, then a=bmodpa=b\mod p.

Proof.

By assumption, we have m=pkm=pk for some kk\in\mathbb{Z}. Moreover, as a=bmodma=b\mod m, then Definition 8.2 means that ab=qma-b=qm for some qq\in\mathbb{Z}. Together, this means ab=qpk=(qk)pa-b=qpk=(qk)p. As qkqk\in\mathbb{Z}, by Definition 8.2, we therefore also have a=bmodpa=b\mod p.

8.2.1 Modular Arithmetic Operations

Now we have objects which are equivalent, we would like to be able to combine these. Just like we can add time together without breaking our clock, we can have addition and multiplication modulo mm.

Proposition 8.8 (Addition and Multiplication).

If mm\in\mathbb{N} and a,b,x,ya,b,x,y\in\mathbb{Z} such that a=xmodma=x\mod m and b=ymodmb=y\mod m, then

  • (a)

    a=xmodm-a=-x\mod m

  • (b)

    a+b=x+ymodma+b=x+y\mod m

  • (c)

    ab=xymodmab=xy\mod m

  • (d)

    ak=xkmodma^{k}=x^{k}\mod m for every kk\in\mathbb{N}.

Proof.

By assumption, mm divides axa-x and mm divides byb-y.

  1. (a)

    Then mm divides (ax)=(a)(x)-(a-x)=(-a)-(-x), so a=xmodm-a=-x\mod m.

  2. (b)

    The Linear Combination Lemma implies that mm divides

    (ax)+(by)=(a+b)(x+y),(a-x)+(b-y)=(a+b)-(x+y),

    and so a+b=xymodma+b=x-y\mod m.

  3. (c)

    Since m|(ax)m|(a-x) and m|(by)m|(b-y), we have a=x+pma=x+pm for some pp\in\mathbb{Z} and b=y+qmb=y+qm for some qq\in\mathbb{Z}. Thus,

    ab=(x+pm)(y+qm)=xy+pmy+xqm+pqm2=xy+m(py+xq+pqm),ab=(x+pm)(y+qm)=xy+pmy+xqm+pqm^{2}=xy+m(py+xq+pqm),

    and so ab=xymodmab=xy\mod m by Theorem 8.4 (part (d)).

  4. (d)

    This can be proven by induction. Let P(n)P(n) be the statement that an=xnmodma^{n}=x^{n}\mod m.

    For the base case of n=1n=1, this follows from the assumption a=xmodma=x\mod m.

    For the induction step, suppose ak=xkmodma^{k}=x^{k}\mod m for some k1k\geq 1. Then by part (c) of this theorem with b=akb=a^{k} and y=xky=x^{k} and using the fact that a=xmodma=x\mod m, we have

    aak=xxkmodm.a\cdot a^{k}=x\cdot x^{k}\mod m.

    Thus,

    ak+1=xk+1modm.a^{k+1}=x^{k+1}\mod m.

    This proves that P(k)P(k+1)P(k)\implies P(k+1) and thus P(n)P(n) holds for all nn\in\mathbb{N} by induction.

Altogether, we can now define the following set.

Definition 8.9.

For mm\in\mathbb{N}, addition and multiplication on the set

/m={0,1,,m1},\mathbb{Z}/m=\{0,1,\ldots,m-1\},

is defined as follows. For every x,y/mx,y\in\mathbb{Z}/m:

  • x+yx+y is the unique z/mz\in\mathbb{Z}/m such that x+y=zmodmx+y=z\mod m.

  • xyx\cdot y is the unique z/mz\in\mathbb{Z}/m such that xy=zmodmx\cdot y=z\mod m.

From this, we can then form addition and multiplication tables.

++ 0 1 2 3 4
0 0 1 2 3 4
1 1 2 3 4 0
2 2 3 4 0 1
3 3 4 0 1 2
4 4 0 1 2 3
×\times 0 1 2 3 4
0 0 0 0 0 0
1 0 1 2 3 4
2 0 2 4 1 3
3 0 3 1 4 2
4 0 4 3 2 1
Figure 8.2: Addition and Multiplication Tables modulo 5

We can then define subtraction.

Proposition 8.10.

Every x/mx\in\mathbb{Z}/m has an additive inverse i.e. there exists y/my\in\mathbb{Z}/m such that x+y=0modmx+y=0\mod m.

Proof.

Let x/mx\in\mathbb{Z}/m and aa\in\mathbb{Z} such that a=xmodma=x\mod m. Then, by Proposition 8.8 (a), we have a=xmodm-a=-x\mod m. Moreover, by Proposition 8.8 (b), aa=0=xxmodma-a=0=x-x\mod m. Finally, from Corollary 8.5, we know there exists a y/my\in\mathbb{Z}/m such that x=ymodm-x=y\mod m.

However, we have to be a bit more careful with the definition of division.

Proposition 8.11 (Division).

Suppose mm\in\mathbb{N} is coprime to aa\in\mathbb{Z}. Then if x,yx,y\in\mathbb{Z} such that xa=yamodmxa=ya\mod m, we have x=ymodmx=y\mod m i.e. we can “divide” both sides of the equation by aa.

Proof.

Suppose aa and mm are coprime and xa=yamodmxa=ya\mod m. Then by definition 8.2, mm divides xaya=(xy)axa-ya=(x-y)a. Since mm and aa are coprime, mm must divides xyx-y, and so x=ymodmx=y\mod m by definition.

This doesn’t work without the coprime condition. Indeed, consider multiplication modulo 6. If we take a=2a=2, then we see that 2=8mod62=8\mod 6 but 14mod61\neq 4\mod 6.

We can also see this from our multiplication tables. With our usual definition of division, ab=a1b=r\frac{a}{b}=a\cdot\frac{1}{b}=r, where 1b\frac{1}{b} is called the multiplicative inverse of bb. This is the unique real number such that b1b=1bb=1b\cdot\frac{1}{b}=\frac{1}{b}\cdot b=1. But the only real number we can’t divide by is 0 i.e. 0 has no multiplicative inverse, because there is no real number that we can multiply by 0 to give us 1.

We can apply a similar thinking to working modulo mm. We can define bb to be the multiplicative inverse of aa modulo mm if ba=ab=1modmba=ab=1\mod m. But notice in your multiplication table modulo 6, say, that some rows (rows 0,2,3,4) don’t contain a 1 in them at all! This means there is no multiplicative inverse in /6\mathbb{Z}/6 for 0, 2, 3 and 4. These are precisely the elements in /6\mathbb{Z}/6 that are not coprime with 6.

×\times 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 1 2 3 4 5
2 0 2 4 0 2 4
3 0 3 0 3 0 3
4 0 4 2 0 4 2
5 0 5 4 3 2 1
Figure 8.3: Multiplication table modulo 6
Corollary 8.12.

If pp is prime, then every x/px\in\mathbb{Z}/p has both an additive and multiplicative inverse.

If a set has both well-defined addition and multiplication, and their inverses, then we call the set a field. See more in Introduction to Mathematical Analysis next term.

8.2.2 Applications of Modular Arithmetic

Congruences do let us solve some nice problems.

Exercise 8.13.

Show that the sequence given by 3n+83n+8, nn\in\mathbb{N} contains no squares.

Solution (please try for yourself before looking)

We want to show that there is no mm\in\mathbb{Z} such that m2=3n+8m^{2}=3n+8 for any nn\in\mathbb{N}. To simplify this, we consider this equation modulo 3 so, recalling 8=2mod38=2\mod 3, we want to solve

m2=2mod3.m^{2}=2\mod 3.

We can use an exhaustive proof here:

Case 1: If a=0, then a2=0mod3.\displaystyle\text{ If }a=0,\text{ then }a^{2}=0\mod 3.
Case 2: If a=1, then a2=1mod3.\displaystyle\text{ If }a=1,\text{ then }a^{2}=1\mod 3.
Case 3: If a=2, then a2=4=1mod3.\displaystyle\text{ If }a=2,\text{ then }a^{2}=4=1\mod 3.

Any other integer is equivalent to one of these cases modulo 3 meaning there is no integer mm such that m2=2mod3m^{2}=2\mod 3.

Exercise 8.14.

Let nn\in\mathbb{Z} with digits arar1a0a_{r}a_{r-1}\ldots a_{0}. Show that nn is divisible by 3 if and only if the sum of the digits is divisible by 3.

Solution (please try for yourself before looking)

Let nn\in\mathbb{Z} with digits arar1a0a_{r}a_{r-1}\ldots a_{0}, then we can write

n=a0+10a1++10rar.n=a_{0}+10a_{1}+\cdots+10^{r}a_{r}.

Note that 10=1mod310=1\mod 3 so any power of 10 is also equal to 1 mod 3. Therefore, considering this equation modulo 3 we get,

n=a0+a1++armod3.n=a_{0}+a_{1}+\cdots+a_{r}\mod 3.

Therefore nn is divisible by 3 (i.e. n=0mod3n=0\mod 3), if and only if the sum of the digits aia_{i} is divisible by 3.

Exercise 8.15.

Prove that the last digit of 7710007^{7^{1000}} is 7.

Solution (please try for yourself before looking)

To find the last digit of a number, we consider it modulo 10.

We therefore first consider powers of 77 modulo 10:

71\displaystyle 7^{1} =7×1=7mod10\displaystyle=7\times 1=7\mod 10
72\displaystyle 7^{2} =7×7=9mod10\displaystyle=7\times 7=9\mod 10
73\displaystyle 7^{3} =7×9=3mod10\displaystyle=7\times 9=3\mod 10
74\displaystyle 7^{4} =7×3=1mod10\displaystyle=7\times 3=1\mod 10
75\displaystyle 7^{5} =7×1=7mod10\displaystyle=7\times 1=7\mod 10
\displaystyle\quad\quad\vdots

So in general, the last digit of 7k7^{k} depends on the value of kmod4k\mod 4. We therefore want to find 71000mod47^{1000}\mod 4. Similarly to above, we have

71\displaystyle 7^{1} =3mod4\displaystyle\hskip 41.25641pt=3\mod 4
72\displaystyle 7^{2} =3×3=1mod4\displaystyle=3\times 3=1\mod 4
73\displaystyle 7^{3} =3×1=3mod4\displaystyle=3\times 1=3\mod 4
\displaystyle\hskip 21.33955pt\vdots

Hence 71000=(72)500=1500=1mod47^{1000}=(7^{2})^{500}=1^{500}=1\mod 4 so 771000=71=7mod107^{7^{1000}}=7^{1}=7\mod 10 and the last digit of 7710007^{7^{1000}} is therefore 77.