The Chinese Remainder Theorem L. Felipe Martins Department of Mathematics Cleveland State University l.martins@csuohio.edu Work licensed under a Creative Commons License available at http://creativecommons.org/licenses/by-nc-sa/3.0/us/ January 30, 2009 1 Initial Examples We start by looking at a simple example. Example 1.1. Suppose we want to find all integers x such that: x 5 pmod 10q (1) x 2 pmod 27q (2) The first equation implies that x 5 10y for some y P Z. Plugging this into the second equation, we get 5 10y 7 pmod 27q, which we can rearrange as 10y 2 5 3 24 pmod 27q. Since gcdp10,27q 1, 10 is invertible modulo 27, so we compute: 10 1 mod 27 19. So, we get: y 24 19 24 pmod 27q. Now we have to plug this back into x 5 10y. We have to be somewhat careful, however, if we want to find all solutions. We have found above infinitely many solutions for y, of the form y 24 27k, for k P Z. From this we get x 5 10y 5 10 p24 27kq 245 270k, for k P Z. Thas is: x 245 pmod 270q 1
We conclude that solutions of the system (1), (2) are the elements of the congruence class r245s, modulo 270 10 27. Another important point to notice is that the soution method presented here requires that 10 and 27 are relatively prime. We now generalize the observations made above, and also introduce a slightly different method of solution. Consider the system: We assume that gcdpm1,m2q 1. Let x x 1 m 2 determined. Notice that: x b 1 pmod m 1 q (3) x b 2 pmod m 2 q (4) x x 1 m 2 x 2 m 1 x 1 m 2 pmod m 1 q x x 1 m 2 x 2 m 1 x 2 m 1 pmod m 2 q Plugging these values into equations (3), (4) we get: x 2 m 1, where x 1 and x 2 are to be x 1 m 2 b 1 pmod m 1 q (5) x 2 m 1 b 2 pmod m 2 q (6) Since m 1 and m 2 are relatively prime, these equations have solutions: x 1 b 1 pm 1 2 mod m 1 q (7) x 2 b 2 pm 1 1 mod m 2 q. (8) The set of all solutions x of (3), (4) is then characterized by: x x 1 m 2 x 2 m 1 pmod m 1 m 2 q where x 1 and x 2 are given by (7) and (8). Example 1.2. Let s solve system (1), (2) by this method, using Sage for the computations: sage: b1,b2,m1,m2 = 5,2,10,27 1,x2 = b1* inverse_mod (m2,m1),b2* inverse_mod (m1,m2) 1,x2 (15, 38) = b1*m2+b2*m1;x 155 %10,x%27 (5, 20) 2
2 The Chinese Remainder Theorem In this section, we extend the method of the previous section to the case of more than two congruences. To get started, consider the following example, with three congruences: Example 2.1. x 4 pmod 6q (9) x 33 pmod 55q (10) x 18 pmod 49q (11) We make the requirement that the moduli are pairwise coprime: gcdp6,55q gcdp6,49q gcdp55,49q 1 (12) We let M be the product of the moduli: M 6 55 49 16170, and let N i M{m i for i 1,2,3: N 1 55 49 2695, N 2 6 49 294, N 3 6 55 330. The numbers N 1, N 2 and N 3 have the following two important properties: 1. gcdpm i,n i q 1, since m 1, m 2, m 3 are pairwise coprime. 2. m i N j if i j We seek a solution to (9), (10), (11) of the form: x x 1 N 1 x 2 N 2 x 3 N 3, where x 1, x 2, x 3 are to be determined. Taking x modulo m i for i 1,2,3, and using property (2) above we have: x x i N i pmod m i q, so that we want: x i N i b i pmod m i q 3
By property (1) above, N i is invertible modulo m i, so that these congruences have the solutions: Thus we have a solution: x 1 b 1 pn 1 1 mod m 1 q 4 4 pmod 6q x 2 b 2 pn 2 1 mod m 2 q 957 22 pmod 55q x 3 b 3 pn 3 1 mod m 3 q 270 25 pmod 18q, x 4 2695 22 294 24 330 25498. This is indeed a solution, as can be verified by substitution in (9), (10), (11). We want, however, to find all solutions of the system. To this end, suppose that x 1 and x 2 are two solutions. Then we have: pmod m 1 q pmod m 2 q pmod m 3 q Since m 1, m 2, m 3 are pairwise coprime, this is equivalent to: mod pmq, where M m 1 m 2 m 3 16170. This means that solutions can be reduced modulo 16170, and the general solution of the system is: x 25498 9328 pmod 16170q. We now state and prove the general theorem. Theorem 2.2 (Chinese Remainder Theorem ( CRT )). Suppose we are give integers b i, m i for 1 i n. Assume that the m i are pairwise coprime: Then, there is a x such that: gcdpm i,m j q 1 for 1 i, j n, ı j. x b i pmod m i q for 1 i n. (13) Furthermore, the solution of the system is unique modulo M m 1 m 2...m n. 4
Proof. To prove existence, let M be as in the statement of the theorem and let N i M{m i for 1 i n. Then, gcdpn i,m i q 1, so N i is invertible modulo m i, and it is possible to find x i such that x i N i b i pmod m i q. We then let: Then, since m i N j if i j, we have x x 1 N 1 x 2 N 2 x n N n. x m i N i b i pmod m i q. To prove uniqueness modulo M, suppose that x 1 and x 2 are both solutions of the system (13). Then, pmod m i q for 1 i n, that is, m i x 1 x 2. Since m 1, m 2,..., m n have no common factors, this implies M m 1 m 2...m n x 1 x 2, that is, pmod Mq. 3 The CRT in Sage We can solve system (13) in Sage by following the steps outlined in the previous section, both in Example 2.1 and in the proof of the CRT. We use lists to represent the problem data, so that our solution can be easily generalized to an arbitrary number of equations. sage: blist = [4,33,18] sage: mlist = [6,55,49] Now, we check that the assumption that the moduli are pairwise coprime: sage: all ([ gcd(u,v)==1 for u,v in zip(mlist,mlist) if u<v]) True Next compute M, the product of the moduli and N i M{m i for i 1,...,n: sage: M=prod(mlist) sage: Nlist =[M//m for m in mlist] sage: M,Nlist (16170, [2695, 294, 330]) Now compute the x i, which are solutions of x i N i b i pmod m i q list = [b* inverse_mod (N,m)%m for b,n,m in zip(blist,nlist,mlist )] list [4, 22, 25] Then, we can find x using: 5
=sum(x*n for x, N in zip(xlist,nlist )) 25498 Of course, we want reduce x modulo M: %= M 9328 Verifying that the computation is correct: sage: [ x % m for m in mlist] [4, 33, 18] Sage has functions to solve CRT systems directly. If we have only two equations, such as the ones in (1) and (2), we can use: =crt (5,2,10,27) 245 % 10, x%27 (5, 2) Notice the order of the arguments in the call: crt(b1,b2,m1,m2) returns a x such that x%m1==b1 and x%m2==b2 (if b1 and b2 are between 0 and x-1). It is said that, in ancient China, the CRT was used to determine the size of armies. Suppose, for example, that, by asking the soldiers to reposition themselves in the field, it is determined that: When the soldiers stand in rows of 7, no soldiers are left. When the soldiers stand in rows of 11, 5 soldiers are left. When the soldiers stand in rows of 13, 8 soldiers are left. When the soldiers stand in rows of 17, 13 soldiers are left. This means that the number of soldiers, x, satisfies: x 0 x 5 x 8 x 13 pmod 7q pmod 11q pmod 13q pmod 17q This can be solved in Sage with: 6
= CRT_list ([0,5,8,13],[7,11,13,17]) 10213 Notice that this method will give the wrong answer if the army has more than 17016 soldiers. Do you see why? What can be done if the army is larger than 17016? 7