Chinese Remainder Theorem¶
The Chinese Remainder Theorem (which will be referred to as CRT in the rest of this article) was discovered by Chinese mathematician Sun Zi.
Formulation¶
Let
where
E.g. the system of congruences
has the solution
Corollary¶
A consequence of the CRT is that the equation
is equivalent to the system of equations
(As above, assume that
Solution for Two Moduli¶
Consider a system of two equations for coprime
We want to find a solution for
In fact
With those two coefficients we can define a solution:
It's easy to verify that this is indeed a solution by computing
Notice, that the Chinese Remainder Theorem also guarantees, that only 1 solution exists modulo
Lets assume that you have two different solutions
Solution for General Case¶
Inductive Solution¶
As
Direct Construction¶
A direct construction similar to Lagrange interpolation is possible.
Let
We can check this is indeed a solution, by computing
Implementation¶
struct Congruence {
long long a, m;
};
long long chinese_remainder_theorem(vector<Congruence> const& congruences) {
long long M = 1;
for (auto const& congruence : congruences) {
M *= congruence.m;
}
long long solution = 0;
for (auto const& congruence : congruences) {
long long a_i = congruence.a;
long long M_i = M / congruence.m;
long long N_i = mod_inv(M_i, congruence.m);
solution = (solution + a_i * M_i % M * N_i) % M;
}
return solution;
}
Solution for not coprime moduli¶
As mentioned, the algorithm above only works for coprime moduli
In the not coprime case, a system of congruences has exactly one solution modulo
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. It's not possible that a number is both odd and even, therefore there is clearly no solution.
It is pretty simple to determine is a system has a solution. And if it has one, we can use the original algorithm to solve a slightly modified system of congruences.
A single congruence
With this fact, we can modify the system of congruences into a system, that only has prime powers as moduli. E.g. the above system of congruences is equivalent to:
Because originally some moduli had common factors, we will get some congruences moduli based on the same prime, however possibly with different prime powers.
You can observe, that the congruence with the highest prime power modulus will be the strongest congruence of all congruences based on the same prime number. Either it will give a contradiction with some other congruence, or it will imply already all other congruences.
In our case, the first congruence
If there are no contradictions, then the system of equation has a solution. We can ignore all congruences except the ones with the highest prime power moduli. These moduli are now coprime, and therefore we can solve this one with the algorithm discussed in the sections above.
E.g. the following system has a solution modulo
The system of congruence is equivalent to the system of congruences:
The only congruence with same prime modulo are
It has the solution
Garner's Algorithm¶
Another consequence of the CRT is that we can represent big numbers using an array of small integers.
Instead of doing a lot of computations with very large numbers numbers, which might be expensive (think of doing divisions with 1000-digit numbers), you can pick a couple of coprime moduli and represent the large number as a system of congruences, and perform all operations on the system of equations.
Any number
By using the above algorithm, you can again reconstruct the large number whenever you need it.
Alternatively you can represent the number in the mixed radix representation:
Garner's algorithm, which is discussed in the dedicated article Garner's algorithm, computes the coefficients