The Chinese Remainder Theorem: why simultaneous congruences can be merged
We view CRT as the number-theoretic incarnation of divide-and-conquer, prove the ring isomorphism Z/mnZ = Z/mZ x Z/nZ, and show how Garner's algorithm avoids overflow in practice.
The Chinese Remainder Theorem (CRT), whose origins trace back to the third-century Chinese mathematical text Sunzi Suanjing, remains a powerful tool in modern cryptography and competitive programming alike. It asserts that a system of congruences with pairwise coprime moduli can be "merged" into a single congruence – and the algebra behind this merging reveals a surprisingly clean structure.
1 The problem of Master Sun
2 The theorem
3 A constructive proof
; gives (since ).
; gives (since ).
; gives (since ).
4 The ring-theoretic meaning: a direct product decomposition
The CRT has a clean algebraic formulation.
5 Garner's algorithm
The direct formula from the constructive proof involves the quantities , which can be astronomically large and prone to overflow. Garner's algorithm avoids this problem.
From : .
Substituting into gives , so .
Each subsequent is computed modulo in the same fashion.
6 Applications in competitive programming
Combining results computed under several prime moduli into a single answer.
Recovering a very large number from its residues modulo several small moduli.
Handling a non-prime modulus by factoring it and computing modulo each prime factor.
7 Takeaway
The CRT establishes a one-to-one correspondence between a remainder modulo the product and a tuple of remainders modulo the individual (pairwise coprime) factors.
Algebraically, – the algebraic incarnation of divide and conquer.
The constructive proof gives an explicit formula for the solution.
Garner's algorithm avoids overflow by computing in a mixed-radix representation.
In the next article, we step into the world of quadratic residues and ask why exactly half of the nonzero elements modulo a prime are perfect squares.
Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.