Section 5.4 Using the Chinese Remainder Theorem
Subsection 5.4.1 Constructing simultaneous solutions
Remember that we are trying to solve the system of equations x≡ai (mod ni). It is important to confirm that all ni are coprime in pairs (or that the set of moduli is mutually coprime, Definition 2.4.9). Then the following steps will lead to a solution. You will find basically this proof in any text; I use the notation in [E.2.1], while that in [E.2.4] basically uses the letter m instead of n.Algorithm 5.4.1.
The following steps not only yield the solution, but mostly indicate the proof as well.
First, let's call the product of the moduli n1n2⋯nk=N.
Take the quotient N/ni and call it ci. It's sort of a “complement” to the ith modulus within the big product N.
-
Now find the inverse of each ci modulo ni. That is, for each i, find a solution di such that
cidi≡1 (mod ni)Notice that this is possible. You can't find an inverse modulo any old thing! But in this case, ci is the product of a bunch of numbers, all of which are coprime to ni, so it is also coprime to ni, as required.
For each i, multiply the three numbers ai⋅ci⋅di.
-
Now add all these products together to get our final answer,
x=a1c1d1+a2c2d2+⋯+akckdk.
What remains is to verify that this works. Go back to the last two steps.
-
Let us evaluate each of the products in the penultimate step (indexed by i) modulo the various nj. That looks bad, but most things cancel because each cj is divisible by ni (except for ci itself).
-
When i≠j, the product modulo ni is thus
ajcjdj≡0 (mod ni). -
Otherwise we can use the definition of inverse, and the product is
aicidi≡ai⋅1≡ai (mod ni)
-
-
To check the final step, for each ni, we can do the entire sum modulo ni. The previous item shows
x≡0+0+⋯+ai+⋯+0 (mod ni).So the sum is definitely a simultaneous solution to all the congruences.
Finally, any other solution x′ has to still fulfill x′≡ai≡x (mod ni), so ni∣x′−x for all moduli ni. Since all ni are relatively prime to each other, N∣x′−x too (if a∣c and b∣c and gcd(a,b)=1, then ab∣c). So x′≡x (mod N), which means x is the only solution modulo N!
Example 5.4.2. A first CRT example.
Let's look at how to solve our original system from Question 5.3.1 using this method. First we write our simultaneous congruences:
x≡1 (mod 5)
x≡2 (mod 6)
x≡3 (mod 7)
We'll follow along with each of the steps in Sage. First, I'll make sure I know all my initial constants (print
ing them to verify). This is step 1.
xxxxxxxxxx
n_1, n_2, n_3 = 5,6,7
a_1, a_2, a_3 = 1,2,3
N = n_1*n_2*n_3
print(n_1, n_2, n_3)
print(a_1, a_2, a_3)
print(N)
Next, I'll put down all the ci, the complements to the moduli, so to speak. Remember, ci=N/ni. This is step 2 above.
xxxxxxxxxx
n_1, n_2, n_3 = 5,6,7
a_1, a_2, a_3 = 1,2,3
N = n_1*n_2*n_3
c_1,c_2,c_3 = N/n_1,N/n_2,N/n_3
print(c_1,c_2,c_3)
Now we need to solve for the inverse of each ci modulo ni. One could do this by hand. For instance,
But that is best done on homework for careful practice; in the text, we might as well use the power of Sage.
xxxxxxxxxx
d_1=inverse_mod(42,5); d_2=inverse_mod(35,6);d_3=inverse_mod(30,7)
print(d_1,d_2,d_3)
That was step 3. Now I'll create each of the big product numbers, as well as their sum, which is steps 4 and 5.
xxxxxxxxxx
n_1, n_2, n_3 = 5,6,7
a_1, a_2, a_3 = 1,2,3
N = n_1*n_2*n_3
d_1=inverse_mod(42,5); d_2=inverse_mod(35,6); d_3=inverse_mod(30,7)
print(a_1*c_1*d_1, a_2*c_2*d_2,a_3*c_3*d_3)
print(a_1*c_1*d_1+a_2*c_2*d_2+a_3*c_3*d_3)
Of course, we don't recognize 836 as our answer. But that is because the solution is only unique modulo N:
xxxxxxxxxx
n_1, n_2, n_3 = 5,6,7
N = n_1*n_2*n_3
print(N)
print(mod(836,N))
Now we see our friend 206, as expected if you successfully tried Question 5.3.1.
Sage note 5.4.3. Printing it out.
When using Sage cells, you might not want only the things in the last line returned to you as output. You can use the print
function to get them to print out, as we have done in the preceding example 5.4.2.
xxxxxxxxxx
a,b,c = 1,2,3
print(a)
print(a,b,c)
This is actually capability in Python itself, not just Sage, so if you have previous experience with Python (or perhaps other languages), it is very important to note print()
is a function. That means means the thing to be printed must be in parentheses, such as print(3)
. Previously (in Sage versions previous to 9.0, and anything else based on Python 2) syntax such as print 3
was allowed, and experienced Sage users may need some time to adjust. If you are new to Sage, no worries!
Example 5.4.4.
Let's try some more interesting moduli for an example to do on your own. Can you follow the template?
x≡1 (mod 6)
x≡11 (mod 35)
x≡3 (mod 11)
xxxxxxxxxx
layout=[['a_1','n_1'],['a_2','n_2'],['a_3','n_3']]) (
def _(a_1=(r'\(a_1\)',1), a_2=(r'\(a_2\)',2), a_3=(r'\(a_3\)',3), n_1=(r'\(n_1\)',5), n_2=(r'\(n_2\)',6), n_3=(r'\(n_3\)',7)):
try:
answer = []
for i in [1..n_1*n_2*n_3]:
if (i%n_1 == a_1) and (i%n_2 == a_2) and (i%n_3 == a_3):
answer.append(i)
string1 = r"<ul><li>$x\equiv %s \text{ (mod }%s)$</li>"%(a_1,n_1)
string2 = r"<li>$x\equiv %s \text{ (mod }%s)$</li>"%(a_2,n_2)
string3 = r"<li>$x\equiv %s \text{ (mod }%s)$</li></ul>"%(a_3,n_3)
pretty_print(html("The simultaneous solutions to "))
pretty_print(html(string1+string2+string3))
if len(answer)==0:
pretty_print(html("are none"))
else:
pretty_print(html("all have the form "))
for ans in answer:
pretty_print(html("$%s$ modulo $%s$"%(ans,n_1*n_2*n_3)))
except ValueError as e:
pretty_print(html("Make sure the moduli are appropriate for solving!"))
pretty_print(html("Sage gives the error message:"))
pretty_print(html(e))
Subsection 5.4.2 A theoretical but highly important use of CRT
The following proposition is an example of one of the many useful things we can do with the CRT.Proposition 5.4.5. Converting to and from coprime moduli.
Suppose that X≡Y (mod N), and N=∏mi, where gcd(mi,mj)=1 for all i≠j. Then we have two directions of equivalence between a congruence and a system of congruences.
Certainly if N divides X−Y, so does a factor of N, so X≡Y (mod mi) for each of the relatively prime factors of N. Thus, solutions to the “big” congruence are also solutions to a system of many little ones.
-
But the CRT allows me to reverse this process. The moduli in question are all coprime to each other, so if we are given a solution pair (Xi,Yi) to each of the congruences
Xi≡Yi (mod mi)then when combined they will give one (!) solution of
X≡Y (mod N)