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.8). Then the following steps will lead to a solution. You will find basically this proof in any text; I use the notation in [C.2.1], while that in [C.2.4] basically uses the letter m instead of n.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 we evaluate each of these products (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)
-
-
Now add all these products together to get our final answer,
x=a1c1d1+a2c2d2+β―+akckdk.For each ni, we can do the sum modulo ni too; the previous step shows this sum is
xβ‘0+0+β―+ai+β―+0 (mod ni).So this is definitely a solution.
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.1. 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).
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.
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)
Now I'll create each of the big product numbers, as well as their sum.
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:
xxxxxxxxxx
n_1, n_2, n_3 = 5,6,7
N = n_1*n_2*n_3
print(N)
print(mod(836,N))
Sage note 5.4.2. 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.1.
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.3.
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.4. 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)