Skip to content

Commit f0a8849

Browse files
committed
add section about not coprime moduli in CRT
1 parent 26ab2e6 commit f0a8849

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

src/algebra/chinese-remainder-theorem.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ $$\begin{array}{rcl}
2121

2222
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

24+
E.g. the system of congruences
25+
26+
$$\begin{array}{rcl}
27+
a & \equiv & 2 \pmod{3} \\
28+
a & \equiv & 3 \pmod{5} \\
29+
& \vdots & \\
30+
a & \equiv & 2 \pmod{7}
31+
\end{array}$$
32+
33+
has the solution $23$ modulo $105$, because $23 \bmod{3} = 2$, $23 \bmod{5} = 3$, and $23 \bmod{7} = 2$.
34+
We can write down every solution as $23 + 105\cdot k$ for $k \in \mathbb{Z}$.
35+
36+
2437
### Corollary
2538

2639
A consequence of the CRT is that the equation
@@ -105,7 +118,7 @@ a & \equiv & \sum_{j=1}^k a_j M_j N_j & \pmod{m_i} \\
105118

106119
```{.cpp file=chinese_remainder_theorem}
107120
struct Congruence {
108-
long long a, m, totient_m;
121+
long long a, m;
109122
};
110123

111124
long long chinese_remainder_theorem(vector<Congruence> const& congruences) {
@@ -126,6 +139,73 @@ long long chinese_remainder_theorem(vector<Congruence> const& congruences) {
126139
}
127140
```
128141

142+
## Solution for not coprime moduli
143+
144+
As mentioned, the algorithm above only works for coprime moduli $m_1, m_2, \dots m_k$.
145+
146+
In the not coprime case, a system of congruences has exactly one solution modulo $\text{lcm}(m_1, m_2, \dots, m_k)$, or has no solution at all.
147+
148+
E.g. in the following system, the first congruence implies that the solution is odd, and the second congruence implies that the solution is even.
149+
It's not possible that a number is both odd and even, therefore there is clearly no solution.
150+
151+
$$\begin{align}
152+
a & \equiv 1 \pmod{4} \\
153+
a & \equiv 2 \pmod{6}
154+
\end{align}$$
155+
156+
It is pretty simple to determine is a system has a solution.
157+
And if it has one, we can use the original algorithm to solve a slightly modified system of congruences.
158+
159+
A single congruence $a \equiv a_i \pmod{m_i}$ is equivalent to the system of congruences $a \equiv a_i \pmod{p_j^{n_j}}$ where $p_1^{n_1} p_2^{n_2}\cdots p_k^{n_k}$ is the prime factorization of $m_i$.
160+
161+
With this fact, we can modify the system of congruences into a system, that only has prime powers as moduli.
162+
E.g. the above system of congruences is equivalent to:
163+
164+
$$\begin{array}{ll}
165+
a \equiv 1 & \pmod{4} \\
166+
a \equiv 2 \equiv 0 & \pmod{2} \\
167+
a \equiv 2 & \pmod{3}
168+
\end{array}$$
169+
170+
Because originally some moduli had common factors, we will get some congruences moduli based on the same $prime$, however possibly with different prime powers.
171+
172+
You can observe, that the congruence with the highest prime power modulus will be the only necessary congruence of all congruences based on the same prime number.
173+
Either it will give a contradiction with some other congruence, or it will imply already all other congruences.
174+
175+
In our case, the first congruence $a \equiv 1 \pmod{4}$ implies $a \equiv 1 \pmod{2}$, and therefore contradicts the second congruence $a \equiv 0 \pmod{2}$.
176+
Therefore this system of congruences has no solution.
177+
178+
If there are no contradictions, then the system of equation has a solution.
179+
We can ignore all congruences except the ones with the highest prime power moduli.
180+
These moduli are now coprime, and therefore we can solve this one with the algorithm discussed in the sections above.
181+
182+
E.g. the following system has a solution modulo $\text{lcm}(10, 12) = 60$.
183+
184+
$$\begin{align}
185+
a & \equiv 3 \pmod{10} \\
186+
a & \equiv 5 \pmod{12}
187+
\end{align}$$
188+
189+
The system of congruence is equivalent to the system of congruences:
190+
191+
$$\begin{align}
192+
a & \equiv 3 \equiv 1 \pmod{2} \\
193+
a & \equiv 3 \equiv 3 \pmod{5} \\
194+
a & \equiv 5 \equiv 1 \pmod{4} \\
195+
a & \equiv 5 \equiv 2 \pmod{3}
196+
\end{align}$$
197+
198+
The only congruence with same prime modulo are $a \equiv 1 \pmod{4}$ and $a \equiv 1 \pmod{2}$.
199+
The first one already implies the second one, so we can ignore it, and solve the following system with coprime moduli instead:
200+
201+
$$\begin{align}
202+
a & \equiv 3 \equiv 3 \pmod{5} \\
203+
a & \equiv 5 \equiv 1 \pmod{4} \\
204+
a & \equiv 5 \equiv 2 \pmod{3}
205+
\end{align}$$
206+
207+
Which has the solution $53 \pmod{60}$, and indeed $53 \bmod{10} = 3$ and $53 \bmod{12} = 5$.
208+
129209
## Garner's Algorithm
130210

131211
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)