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 equationsAlgorithm 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
Take the quotient
and call it It's sort of a “complement” to the th modulus within the big product-
Now find the inverse of each
modulo That is, for each find a solution such thatNotice that this is possible. You can't find an inverse modulo any old thing! But in this case,
is the product of a bunch of numbers, all of which are coprime to so it is also coprime to as required. For each
multiply the three numbers-
Now add all these products together to get our final answer,
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
) modulo the various That looks bad, but most things cancel because each is divisible by (except for itself).-
When
the product modulo is thus -
Otherwise we can use the definition of inverse, and the product is
-
-
To check the final step, for each
we can do the entire sum modulo The previous item showsSo the sum is definitely a simultaneous solution to all the congruences.
Finally, any other solution
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:
(mod ) (mod ) (mod )
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
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
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
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
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 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?
(mod ) (mod ) (mod )
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
Certainly if
divides so does a factor of so (mod ) for each of the relatively prime factors of 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
to each of the congruencesthen when combined they will give one (!) solution of