← back to Number Theory

Chinese Remainder Theorem

Jim Hefferon · Elementary Number Theory · Ch. 5

If moduli are pairwise coprime, a system of congruences has a unique solution mod their product. The Chinese Remainder Theorem gives a constructive algorithm to find it.

Overlapping cycles

Think of CRT as synchronizing clocks with different periods. A cycle of length 3 and a cycle of length 5 meet every 15 steps. The meeting point is the unique solution.

Cycles of length 3 and 5 synchronize at position 2 mod 3: 0 1 2 0 1 2 0 1 2 0 1 mod 5: 0 1 2 3 4 0 1 2 3 4 0 0 1 2 3 4 5 6 7 8 9 10

The algorithm

Given x ≡ a1 (mod n1), x ≡ a2 (mod n2), ..., with pairwise coprime ni: compute N = n1 n2 ... nk. For each i, let Ni = N/ni. Find yi such that Ni yi ≡ 1 (mod ni) using extended GCD. Then x ≡ sum of ai Ni yi (mod N).

Scheme

Applications

CRT speeds up RSA by splitting computation mod p and mod q separately (smaller numbers, then recombine). It also explains why calendar cycles repeat: the day-of-week cycle (7) and day-of-month cycle interact via CRT.

Scheme