Home page for accesible maths 5 Congruences

Style control - access keys in brackets

Font (2 3) - + Letter spacing (4 5) - + Word spacing (6 7) - + Line spacing (8 9) - +

5.3 The Chinese Remainder Theorem

In the previous section we saw how to decide if certain congruences have solutions, and how to solve them when solutions exist. In this section we shall consider a certain type of problem in which two congruences are involved. We begin with an example.

Example 5.3.1

I have a CD collection numbering under 100100, and recently I reorganized it. Originally the CDs were arranged in sets of 1010, and there were 77 left over. My new arrangement groups 1313 together at a time, and now there are 99 left over. How large is my collection?

How can we try to solve this problem? Clearly if xx is the answer then it satisfies the two congruences

x7mod10  and  x9mod13.x\equiv 7\bmod 10\qquad\hbox{and}\qquad x\equiv 9\bmod 13.

One approach would be to take the solutions to the first of these and work through them in turn until we find one which also satisfies the second. Thus we would work as follows:

010+7=77=013+7not a solution110+7=1717=113+4not a solution210+7=2727=213+1not a solution310+7=3737=213+11not a solution410+7=4747=313+8not a solution510+7=5757=413+5not a solution610+7=6767=513+2not a solution710+7=7777=513+12not a solution810+7=8787=613+9not a solution!910+7=9797=713+6not a solution\begin{array}[]{rlrll}0\cdot 10+7=&\!\!\!\phantom{0}7&7&=0\cdot 13+7&\hbox{not% a solution}\\ 1\cdot 10+7=&\!\!\!17&17&=1\cdot 13+4&\hbox{not a solution}\\ 2\cdot 10+7=&\!\!\!27&27&=2\cdot 13+1&\hbox{not a solution}\\ 3\cdot 10+7=&\!\!\!37&37&=2\cdot 13+11&\hbox{not a solution}\\ 4\cdot 10+7=&\!\!\!47&47&=3\cdot 13+8&\hbox{not a solution}\\ 5\cdot 10+7=&\!\!\!57&57&=4\cdot 13+5&\hbox{not a solution}\\ 6\cdot 10+7=&\!\!\!67&67&=5\cdot 13+2&\hbox{not a solution}\\ 7\cdot 10+7=&\!\!\!77&77&=5\cdot 13+12&\hbox{not a solution}\\ 8\cdot 10+7=&\!\!\!87&87&=6\cdot 13+9&\hbox{\phantom{not} a solution!}\\ 9\cdot 10+7=&\!\!\!97&97&=7\cdot 13+6&\hbox{not a solution}\end{array}

Since the problem specified that the number of CDs was less than 100100, we see that the unique solution is 87.87.

In general the problem will be to solve simultaneously the pair of congruences

xamodm  and  xbmodn.x\equiv a\bmod m\qquad\hbox{and}\qquad x\equiv b\bmod n. (5.3.1)

The trial-and-error approach proved successful in the case above, but if the numbers were larger it would involve rather a lot of work. We shall now try to produce a better approach.

Let us consider the case where the moduli mm and nn are coprime, and take r,sr,s\in\mathbb{Z} such that rm+sn=1rm+sn=1. If we multiply this identity by aa, then we obtain arm+asn=aarm+asn=a and thus asnamodmasn\equiv a\bmod m. On the other hand, multiplying rm+sn=1rm+sn=1 by bb, we get brm+bsn=bbrm+bsn=b, so that brmbmodnbrm\equiv b\bmod n. Hence x=asnx=asn is a solution to the first congruence, while x=brmx=brm solves the other. Adding up these two separate solutions, we obtain a number c=asn+brmc=asn+brm which satisfies

c=asn+brmasnamodmc=asn+brm\equiv asn\equiv a\bmod m

and

c=asn+brmbrmbmodn.c=asn+brm\equiv brm\equiv b\bmod n.

Thus cc solves the two congruences simultaneously.

In fact, we can build on this idea to obtain the following description of all simultaneous solutions to the pair of congruences (5.3.1) (still under the assumption that the moduli mm and nn are coprime); this result is known as the Chinese Remainder Theorem.

Theorem 5.3.2

Let a,ba,b\in\mathbb{Z}, let m,nm,n\in\mathbb{N} be coprime, and write rm+sn=1rm+sn=1 for some r,sr,s\in\mathbb{Z}. Then an integer xx satisfies xamodmx\equiv a\bmod m and xbmodnx\equiv b\bmod n if and only if xasn+brmmodmnx\equiv asn+brm\bmod mn.

Example 5.3.3

Solve the pair of congruences x2mod8x\equiv 2\bmod 8 and x5mod9x\equiv 5\bmod 9.

Solution. Since hcf(8,9)=1\mathrm{hcf}(8,9)=1, the Chinese Remainder Theorem applies. We have a=2a=2, b=5b=5, m=8m=8 and n=9n=9. We require integers rr and ss such that 8r+9s=18r+9s=1; clearly we can take r=-1r=-1 and s=1s=1 here. We now compute mn=89=72mn=8\cdot 9=72 and

asn+brm\displaystyle asn+brm =219+5(-1)8=18-40=-22\displaystyle=2\cdot 1\cdot 9+5(-1)8=18-40=-22
-22+72=50mod72,\displaystyle\equiv-22+72=50\bmod 72,

so that the complete solution to the pair of congruences is x50mod72x\equiv 50\bmod 72. (As a check, we observe that 50-2=48=6850-2=48=6\cdot 8 and 50-5=45=5950-5=45=5\cdot 9, so that 502mod850\equiv 2\bmod 8 and 505mod950\equiv 5\bmod 9.)

Proof. Set c=asn+brmc=asn+brm\in\mathbb{Z}. We saw above that camodmc\equiv a\bmod m and cbmodnc\equiv b\bmod n. Combining this with Lemmas 5.1.7 and 5.1.9, we see that xx\in\mathbb{Z} satisfies xamodmx\equiv a\bmod m and xbmodnx\equiv b\bmod n if and only if xcmodmx\equiv c\bmod m and xcmodnx\equiv c\bmod n. By definition, this means that m|x-cm|x-c and n|x-cn|x-c, or in other words that x-cx-c is a common multiple of mm and nn. Now Theorem 4.5.5 states that the common multiples of mm and nn are exactly the multiples of lcm(m,n),\mathrm{lcm}(m,n), and lcm(m,n)=mn\mathrm{lcm}(m,n)=mn because mm and nn are coprime. Hence we conclude that xx\in\mathbb{Z} satisfies xamodmx\equiv a\bmod m and xbmodnx\equiv b\bmod n if and only if mn|x-cmn|x-c, that is, if and only if xcmodmnx\equiv c\bmod mn. \Box

Example 5.3.4

Find the smallest positive integer which satisfies the two congruences x7mod9x\equiv 7\bmod 9 and x3mod11x\equiv 3\bmod 11 simultaneously.

Solution. We use the Euclidean algorithm to find the highest common factor of 99 and 1111 and express it as an integral linear combination of them:

11\displaystyle 11 =19+2    and\displaystyle=1\cdot 9+2\qquad\quad\text{and} 1\displaystyle 1 =9-42\displaystyle=9-4\cdot 2
9\displaystyle 9 =42+1\displaystyle=4\cdot 2+1 =9-4(11-9)=59-411.\displaystyle=9-4(11-9)=5\cdot 9-4\cdot 11.

Hence 99 and 1111 are coprime, so the Chinese Remainder Theorem applies, and in the notation introduced therein, we have a=7a=7, b=3b=3, m=9m=9, n=11n=11, r=5r=5 and s=-4s=-4. Thus the pair of congruences has the solution

xasn+brm=7(-4)11+359mod911, that is,x-173mod99.x\equiv asn+brm=7(-4)11+3\cdot 5\cdot 9\bmod 9\cdot 11,\quad\text{that is,}% \quad x\equiv-173\bmod 99.

Now -173=(-2)99+25-173=(-2)99+25, so this simplifies to x25mod99x\equiv 25\bmod 99, and therefore x=25x=25 is the smallest positive simultaneous solution to the two congruences.

As we have seen, the congruences (5.3.1) can be solved simultaneously whenever the moduli mm and nn are coprime. We shall now give a complete description of when simultaneous solutions exist.

Proposition 5.3.5

Let a,ba,b\in\mathbb{Z} and m,nm,n\in\mathbb{N}. Then the two congruences

xamodm  𝑎𝑛𝑑  xbmodnx\equiv a\bmod m\qquad\text{and}\qquad x\equiv b\bmod n

can be solved simultaneously if and only if hcf(m,n)\mathrm{hcf}(m,n) divides a-ba-b.

Example 5.3.6
  1. (i)

    The pair of congruences

    x3mod6  and  x7mod8x\equiv 3\bmod 6\qquad\text{and}\qquad x\equiv 7\bmod 8

    can be solved simultaneously because hcf(6,8)=2\mathrm{hcf}(6,8)=2 divides 3-7=-4.3-7=-4.

  2. (ii)

    On the other hand, the pair of congruences

    x3mod6  and  x7mod9x\equiv 3\bmod 6\qquad\text{and}\qquad x\equiv 7\bmod 9

    cannot be solved simultaneously because hcf(6,9)=3\mathrm{hcf}(6,9)=3 does not divide 3-7=-4.3-7=-4.

Proof. Set d=hcf(m,n)d=\mathrm{hcf}(m,n).

‘‘\Rightarrow’’. Suppose that the two congruences can be solved simultaneously, so that there exists xx\in\mathbb{Z} such that xamodmx\equiv a\bmod m and xbmodnx\equiv b\bmod n; write x-a=pmx-a=pm and x-b=qnx-b=qn, where p,qp,q\in\mathbb{Z}. Then a=x-pma=x-pm and b=x-qnb=x-qn, so that

a-b=(x-pm)-(x-qn)=x-pm-x+qn=qn-pm.a-b=(x-pm)-(x-qn)=x-pm-x+qn=qn-pm.

Since dd is a common factor of mm and nn, Lemma 4.2.13 implies that dd is also a factor of qn-pmqn-pm, and hence of a-b.a-b.

‘‘\Leftarrow’’. Suppose that dd divides a-ba-b, say a-b=dqa-b=dq for some qq\in\mathbb{Z}. By Bézout’s Theorem, we can write d=rm+snd=rm+sn, where r,sr,s\in\mathbb{Z}. Define c=a-qrmc=a-qrm\in\mathbb{Z}. Then clearly camodmc\equiv a\bmod m (because c-a=-qrmc-a=-qrm), and also cbmodnc\equiv b\bmod n because

c-b=a-qrm-b=(a-b)-qrm=dq-qrm=q(d-rm)=qsn,c-b=a-qrm-b=(a-b)-qrm=dq-qrm=q(d-rm)=qsn,

so that cc solves the two congruences simultaneously. \Box