You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algebra/chinese-remainder-theorem.md
+47-25Lines changed: 47 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -10,16 +10,16 @@ The Chinese Remainder Theorem (which will be referred to as CRT in the rest of t
10
10
11
11
## Formulation
12
12
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
14
14
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}$$
21
21
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$.
23
23
24
24
### Corollary
25
25
@@ -29,11 +29,11 @@ $$x \equiv a \pmod{m}$$
29
29
30
30
is equivalent to the system of equations
31
31
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}$$
37
37
38
38
(As above, assume that $m = m_1 m_2 \cdots m_k$ and $m_i$ are pairwise coprime).
39
39
@@ -50,35 +50,57 @@ $$
50
50
51
51
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
52
52
53
-
$$n_1 m_1 + n_2 m_2 = 1$$
53
+
$$n_1 m_1 + n_2 m_2 = 1.$$
54
54
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}$.
56
57
57
-
Then a solution will be
58
+
With those two coefficients we can define a solution:
58
59
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}$$
60
61
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}.
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.
62
76
63
77
## Solution for General Case
64
78
65
79
### Inductive Solution
66
80
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.
68
84
69
85
### Direct Construction
70
86
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.
72
88
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}$.
0 commit comments