Before we launch into a discussion on solving the Chinese Remainder Theorem, we have to understand how to solve linear congruences. In algebra, a linear equation generally takes the form ax = b, where a and b are real numbers. A linear congruence takes the form
ax ≡ b (mod n)
where a, b are given integers and n is a given positive integer. The difference between a linear congruence and an algebraic linear equation is that linear congruences have more than one solution.
Theorem:
If d = gcd(a, n), then the linear congruence
ax ≡ b (mod n)
has a solution if and only if d divides b. If d does divide b, and if x0 is any solution, then the general solution is given by
x = x0 + (nt / d)
where t ∈ Z.
Example 1:
Let’s consider the congruence.
10x ≡ 6 (mod 12)
Here a = 10, b = 6 and n = 12, so d = gcd(10, 12) = 2. 2 divides b = 6, so there are two classes of solutions. Taking x
0 = 3 as a particular solution, so the general solution has the form
x = x0 + (nt / d)
= 3 + (12t / 2)
= 3 + 6t where t ∈ Z
To make this clearer, let’s look at another example with a step-by-step walk-through.
Example 2:
Consider the congruence,
10x ≡ 6 (mod 14)
Step 1:
Calculate d = gcd(a, n) and see if d divides b. If b doesn’t divide d we stop, otherwise we move to step 2. In this example, d = gcd(10, 14) = 2, which divides 6, so solutions do exist.
If x
0 is any solution, then the general solution is
x = x0 + (14/2)t
= x0 + 7t, where t ∈ Z
Step 2:
To find x
0, we divide the original congruence through by gcd(10, 14) = 2 to give
5x ≡ 3 (mod 7)
Step 3:
Since gcd(5, 3) = 1, we note that 3 ≡ 10 (mod 7), with 10 divisible by 5, we replace the congruence with
5x ≡ 10 (mod 7)
And then divide by 5, which is coprime to 7, to give
x ≡ 2 (mod 7)
Thus x
0 = 2 is a solution, so the general solution has the form
x = 2 + 7t (t ∈ Z)
Chinese Remainder Theorem
The Chinese remainder theorem is a system of simultaneous linear congruences.
Theorem:
Let n1, n2,…, nk be pairwise relatively prime positive integers, with gcd(ni, nj) = 1 whenever i ≠ j, and let a1, a2,…, ak be any integers. Then the solutions of the simultaneous congruences
x ≡ a1 (mod n1), x ≡ a2 (mod n2),… x ≡ ak (mod nk)
form a single congruence class (mod n), where n = n1n2…nk.
Example 1:
Solve the system of congruences;
3x ≡ 2 (mod 5)
7x ≡ 4 (mod 11)
8x ≡ 3 (mod 19)
Step 1:
Find n by multiplying the 3 moduli together as n can be factored into prime powers.
n = 5 * 11 * 19
= 1045
Step 2:
Simplify each congruence so it is in the form x ≡ b (mod n). We use the
extended Euclidian algorithm.
3x ≡ 2 (mod 5)
Extended Euclid: 5 = 3(1) + 2
3 = 2(1) + 1
=> 1 = 3 – 2
= 3 – (5 – 3)
= 3(2) + (-1)5
Since 3 is multiplied by 2, we multiply the whole congruence by 2 to get rid of the 3, leaving x on its own.
2*3x ≡ 2*2 (mod 5)
x ≡ 4 (mod 5)
x ≡ -1 (mod 5)
Repeat for the other 2 congruences.
-3*7x ≡ -3*4 (mod 11)
x ≡ -12 (mod 11)
x ≡ -1 (mod 11)
-7*8x ≡ -7*3 (mod 19)
x ≡ -21 (mod 19)
x ≡ -2 (mod 19)
Step 3:
Next we want to use the following formula to determine the overall congruence:
x ≡ a
1p
2 a
3d
1 + a
2 p
1p
3d
2 + a
3 p
1p
2d
3 (mod n)
We already know n = p
1p
2p
3 so p
1 = 5, p
2 = 11 and p
3 = 19. The numbers we have just calculated from the 3 congruences are a
1 = -1, a
2 = -1 and a
3 = (-2).
x ≡ (-1)*11*19*d
1 + (-1)*5*19*d
2 + (-2)*5*11*d
3 (mod 1045)
All that remains is to calculate the d values.
11*19d
1 ≡ d
1 (mod 5)
209d
1 ≡ -1 (mod 5)
d
1 ≡ -1 (mod 5)
5*19d
2 ≡ d
2 (mod 11)
95d
2 ≡ 7 (mod 11)
d
2 ≡ 7 (mod 11)
5*11d
3 ≡ d
3 (mod 19)
55d
3 ≡ -2 (mod 19)
d
3 ≡ -2 (mod 19)
Step 4:
Substitute the d values into the equation and simplify.
x ≡ (-1)*11*19*(-1) + (-1)*5*19*7 + (-2)*5*11*(-2) (mod 1045)
x ≡ 209 - 665 + 220 (mod 1045)
x ≡ -236 (mod 1045)
x ≡ 809 (mod 1045)
Written by JaneW