Two days ago, I spent several hours trying to understand the Chinese remainder theorem because most explanations I could find are a waste of disk space. It’s either unreadable overcomplicated proofs or purely practical step-wise algorithms with little to no justification.
Nobody should have to suffer through explainations that outsource thinking to the reader, so here’s one that doesn’t.
Given a system of modular equations:
solve it for
-
$m = \set{m_1, m_2, \ldots, m_n}$ , -
$m$ are pairwise coprime, -
$M = \prod m$ .
Since we want to get different results depending on the modulus, let’s build
For every
So the first step to building
Taking
Now, we don’t need the actual values of
However, because we still want our filtering to work, we will divide by multiplying by the inverse
To find each inverse, simply use the extended Euclidean algorithm.
And, of course, the last step is to multiply every
-
$M = 2 \times 3 \times 5 \times 7 = 210$ . -
Find
$M_i$ ,$M_i^{-1}$ and the summands of$x$ :$a_i$ $m_i$ $M_i = \frac{M}{m_i}$ $M_i^{-1} \pmod{m_i}$ $a_i \times M_i \times M_i^{-1}$ $1$ $2$ $\frac{210}{2} = 105$ $1$ $1 \times 105 \times 1 = 105$ $2$ $3$ $\frac{210}{3} = 70$ $1$ $2 \times 70 \times 1 = 140$ $4$ $5$ $\frac{210}{5} = 42$ $3$ $4 \times 42 \times 3 = 504$ $6$ $7$ $\frac{210}{7} = 30$ $4$ $6 \times 30 \times 4 = 720$ -
$x \equiv 105 + 140 + 504 + 720 = 1469 \equiv 209 \pmod{210}$ .