Skip to content

Commit 28f416a

Browse files
authored
Merge pull request cp-algorithms#1016 from jxu/patch-8
Chinese Remainder Theorem solutions
2 parents be37d0f + 59fa075 commit 28f416a

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

src/algebra/chinese-remainder-theorem.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Let $m = m_1 \cdot m_2 \cdots m_k$, where $m_i$ are pairwise coprime. In additio
1515
$$\begin{align}
1616
a &\equiv a_1 \pmod{m_1} \\
1717
a &\equiv a_2 \pmod{m_2} \\
18-
&\ldots \\
18+
& \vdots \\
1919
a &\equiv a_k \pmod{m_k}
2020
\end{align}$$
2121

@@ -31,12 +31,59 @@ is equivalent to the system of equations
3131

3232
$$\begin{align}
3333
x &\equiv a_1 \pmod{m_1} \\
34-
&\ldots \\
34+
&\vdots \\
3535
x &\equiv a_k \pmod{m_k}
3636
\end{align}$$
3737

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

40+
## Solution for Two Moduli
41+
42+
Consider a system of two equations for coprime $m_1, m_2$:
43+
44+
$$
45+
\begin{align}
46+
a &\equiv a_1 \pmod{m_1} \\
47+
a &\equiv a_2 \pmod{m_2} \\
48+
\end{align}
49+
$$
50+
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+
53+
$$n_1 m_1 + n_2 m_2 = 1$$
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}$.
56+
57+
Then a solution will be
58+
59+
$$a = a_1 n_2 m_2 + a_2 n_1 m_1$$
60+
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+
63+
## Solution for General Case
64+
65+
### Inductive Solution
66+
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.
68+
69+
### Direct Construction
70+
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
72+
73+
$$N_i M_i + n_i m_i = 1$$
74+
75+
Then a solution to the system of congruences is
76+
77+
$$a = \sum_{i=1}^k a_i N_i M_i$$
78+
79+
Again as $N_i \equiv M_i^{-1} \pmod{m_i}$, the solution is equivalent to
80+
81+
$$a = \sum_{i=1}^k a_i M_i (M_i^{-1} \mod{m_i})$$
82+
83+
Observe $M_i$ is a multiple of $m_j$ for $i \neq j$, and
84+
85+
$$a \equiv a_i N_i M_i \equiv a_i (1 - n_i m_i) \equiv a_i \pmod{m_i}$$
86+
4087
## Garner's Algorithm
4188

4289
Another consequence of the CRT is that we can represent big numbers using an array of small integers. For example, let $p$ be the product of the first $1000$ primes. From calculations we can see that $p$ has around $3000$ digits.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy