Skip to content

Commit e50c9e2

Browse files
committed
improve CRT formulations/math and extend proof
1 parent 28f416a commit e50c9e2

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

src/algebra/chinese-remainder-theorem.md

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ The Chinese Remainder Theorem (which will be referred to as CRT in the rest of t
1010

1111
## Formulation
1212

13-
Let $m = m_1 \cdot m_2 \cdots m_k$, where $m_i$ are pairwise coprime. In addition to $m_i$, we are also given a set of congruence equations
13+
Let $m = m_1 \cdot m_2 \cdots m_k$, where $m_i$ are pairwise coprime. In addition to $m_i$, we are also given a system of congruences
1414

15-
$$\begin{align}
16-
a &\equiv a_1 \pmod{m_1} \\
17-
a &\equiv a_2 \pmod{m_2} \\
18-
& \vdots \\
19-
a &\equiv a_k \pmod{m_k}
20-
\end{align}$$
15+
$$\begin{array}{rcl}
16+
a & \equiv & a_1 \pmod{m_1} \\
17+
a & \equiv & a_2 \pmod{m_2} \\
18+
& \vdots & \\
19+
a & \equiv & a_k \pmod{m_k}
20+
\end{array}$$
2121

22-
where $a_i$ are some given constants. The original form of CRT then states that the given set of congruence equations always has *one and exactly one* solution modulo $m$.
22+
where $a_i$ are some given constants. The original form of CRT then states that the given system of congruences always has *one and exactly one* solution modulo $m$.
2323

2424
### Corollary
2525

@@ -29,11 +29,11 @@ $$x \equiv a \pmod{m}$$
2929

3030
is equivalent to the system of equations
3131

32-
$$\begin{align}
33-
x &\equiv a_1 \pmod{m_1} \\
34-
&\vdots \\
35-
x &\equiv a_k \pmod{m_k}
36-
\end{align}$$
32+
$$\begin{array}{rcl}
33+
x & \equiv & a_1 \pmod{m_1} \\
34+
& \vdots & \\
35+
x & \equiv & a_k \pmod{m_k}
36+
\end{array}$$
3737

3838
(As above, assume that $m = m_1 m_2 \cdots m_k$ and $m_i$ are pairwise coprime).
3939

@@ -50,35 +50,57 @@ $$
5050

5151
We want to find a solution for $a \pmod{m_1 m_2}$. Using the [Extended Euclidean Algorithm](extended-euclid-algorithm.md) we can find Bézout coefficients $n_1, n_2$ such that
5252

53-
$$n_1 m_1 + n_2 m_2 = 1$$
53+
$$n_1 m_1 + n_2 m_2 = 1.$$
5454

55-
Equivalently, $n_1 m_1 \equiv 1 \pmod{m_2}$ so $n_1 \equiv m_1^{-1} \pmod{m_2}$, and vice versa $n_2 \equiv m_2^{-1} \pmod{m_1}$.
55+
In fact $n_1$ and $n_2$ are just the [modular inverses](module-inverse.md) of $m_1$ and $m_2$ modulo $m_2$ and $m_1$.
56+
We have $n_1 m_1 \equiv 1 \pmod{m_2}$ so $n_1 \equiv m_1^{-1} \pmod{m_2}$, and vice versa $n_2 \equiv m_2^{-1} \pmod{m_1}$.
5657

57-
Then a solution will be
58+
With those two coefficients we can define a solution:
5859

59-
$$a = a_1 n_2 m_2 + a_2 n_1 m_1$$
60+
$$a = a_1 n_2 m_2 + a_2 n_1 m_1 \bmod{m_1 m_2}$$
6061

61-
We can easily verify $a = a_1 (1 - n_1 m_1) + a_2 n_1 m_1 \equiv a_1 \pmod{m_1}$ and vice versa.
62+
It's easy to verify that this is indeed a solution by computing $a \bmod{m_1}$ and $a \bmod{m_2}.
63+
64+
$$\begin{array}{rcll}
65+
a & \equiv & a_1 n_2 m_2 + a_2 n_1 m_1 & \pmod{m_1}\\
66+
& \equiv & a_1 (1 - n_1 m_1) + a_2 n_1 m_1 & \pmod{m_1}\\
67+
& \equiv & a_1 & \pmod{m_1}
68+
\end{array}$$
69+
70+
Notice, that the Chinese Remainder Theorem also guarantees, that only 1 solution exists modulo $m_1 m_2$.
71+
This is also easy to prove.
72+
73+
Lets assume that you have two different solutions $x$ and $y$.
74+
Because $x \equiv a_i \pmod{m_i}$ and $y \equiv a_i \pmod{m_i}$, it follows that $x − y \equiv 0 \pmod{m_i}$ and therefore $x − y \equiv 0 \pmod{m_1 m_2}$ or $x \equiv y \pmod{m_1 m_2}$.
75+
So $x$ and $y$ are actually the same solution.
6276

6377
## Solution for General Case
6478

6579
### Inductive Solution
6680

67-
As $m_1 m_2$ is coprime to $m_3$, we can inductively repeatedly apply the solution for two moduli for any number of moduli. For example, combine $a \equiv b_2 \pmod{m_1 m_2}$ and $a \equiv a_3 \pmod{m_3}$ to get $a \equiv b_3 \pmod{m_1 m_2 m_3}$, etc.
81+
As $m_1 m_2$ is coprime to $m_3$, we can inductively repeatedly apply the solution for two moduli for any number of moduli.
82+
First you compute $b_2 := a \pmod{m_1 m_2}$ using the first two congruences,
83+
then you can compute $b_3 := a \pmod{m_1 m_2 m_3}$ using the congruences $a \equiv b_2 \pmod{m_1 m_2}$ and $a \equiv a_3 \pmod {m_3}$, etc.
6884

6985
### Direct Construction
7086

71-
A direct construction similar to Lagrange interpolation is possible. Let $M_i = \prod_{i \neq j} m_j$, the product of all moduli but $m_i$. Again with the Extended Euclidean algorithm we can find $N_i, n_i$ such that
87+
A direct construction similar to Lagrange interpolation is possible.
7288

73-
$$N_i M_i + n_i m_i = 1$$
89+
Let $M_i := \prod_{i \neq j} m_j$, the product of all moduli but $m_i$, and $N_i$ the modular inverses $N_i := M_i^{-1} \bmod{m_i}$.
90+
Then a solution to the system of congruences is:
7491

75-
Then a solution to the system of congruences is
92+
$$a = \sum_{i=1}^k a_i M_i N_i \bmod{m_1 m_2 \dots m_k}$$
7693

77-
$$a = \sum_{i=1}^k a_i N_i M_i$$
94+
We can check this is indeed a solution, by computing $a \bmod{m_i}$ for all $i$.
95+
Because $M_j$ is a multiple of $m_i$ for $i \neq j$ we have
7896

79-
Again as $N_i \equiv M_i^{-1} \pmod{m_i}$, the solution is equivalent to
97+
$$\begin{array}{rcll}
98+
a & \equiv & \sum_{j=1}^k a_j M_j N_j & \pmod{m_i} \\
99+
& \equiv & a_i M_i N_i & \pmod{m_i} \\
100+
& \equiv & a_i M_i M_i^{-1} & \pmod{m_i} \\
101+
& \equiv & a_i & \pmod{m_i}
102+
\end{array}$$
80103

81-
$$a = \sum_{i=1}^k a_i M_i (M_i^{-1} \mod{m_i})$$
82104

83105
Observe $M_i$ is a multiple of $m_j$ for $i \neq j$, and
84106

0 commit comments

Comments
 (0)