Chinese Remainder Theorem
Merge modular congruences with inverses to reconstruct the unique solution modulo the product.
Core idea
For pairwise coprime moduli, construct one basis term per congruence. That term is one under its own modulus and zero under all others, so weighted terms combine independently.
Read the visualization
Each table row builds its partial product, modular inverse, and residue term. The final row adds them and reduces the sum by the full modulus product.
| r | m | Mᵢ | tᵢ | term | |
|---|---|---|---|---|---|
| congruence ① | 2 | 3 | |||
| congruence ② | 3 | 5 | |||
| congruence ③ | 2 | 7 | |||
| total |
Multiply the pairwise coprime moduli to obtain the combined period M.
Complexity and tradeoffs
Time: O(k log M). Space: O(k). The direct form assumes pairwise coprime moduli; generalized merging checks gcd compatibility.
Where it fits
CRT reconstructs values from modular channels, accelerates large-integer arithmetic, and schedules repeating cycles. Non-coprime moduli require compatibility checks and generalized merging.