A system of congruences with coprime moduli always has a unique solution modulo their product.
IntuitionCounting soldiers without counting past 7
A general lines up his soldiers in rows of 3 and sees 2 left over; in rows of 5, 3 are left over; in rows of 7, 2 are left over. How many soldiers are there? Even though the general never counted past 7, those three tiny remainders (2,3,2) pin down the exact count modulo 3×5×7=105: the answer is 23 (plus any multiple of 105). This is the Chinese remainder theorem (CRT): as long as the moduli share no common factor in pairs, knowing the remainders modulo each mi carries the exact same information as knowing the remainder modulo their product m1m2⋯mk. It lets us split one hard computation modulo a huge number into several easy, independent computations modulo its prime-power factors, and then reassemble the result.
A network diagram illustrating how a problem modulo M splits into independent subproblems modulo its coprime factors and recombines.
Residues modulo m=15=3×5: each residue xmod15 is uniquely determined by its pair of remainders (xmod3,xmod5).
SchoolStatement and explicit formula
Definition: A system of simultaneous congruences
Given moduli m1,…,mk that are pairwise coprime — gcd(mi,mj)=1(i=j) — and any integers a1,…,ak, the system x≡ai(modmi)(1≤i≤k) asks for an integer x that simultaneously leaves remainder ai modulo mi for every i. Setting M=m1m2⋯mk,Mi=M/mi and letting yi be the inverse of Mi modulo mi (so Miyi≡1(modmi), which exists since gcd(Mi,mi)=1), the solution is given explicitly by x≡∑i=1kaiMiyi(modM).
x≡ai(modmi)(1≤i≤k),gcd(mi,mj)=1(i=j)
Each term aiMiyi in the sum is engineered to be ≡ai(modmi) (since Miyi≡1(modmi)) and ≡0(modmj) for every j=i (since mj is a factor of Mi=M/mi). So when we reduce the whole sum modulo a fixed mi, all terms except the i-th vanish, leaving ai — exactly as required.
If m1,…,mk are pairwise coprime (gcd(mi,mj)=1(i=j)), then for any integers a1,…,ak the system x≡ai(modmi)(1≤i≤k) has a solution x, and any two solutions are congruent modulo M=m1⋯mk.
Why is it true?
Because the moduli share no prime factors, the condition of leaving remainder a1 modulo m1 and the condition of leaving remainder a2 modulo m2 are completely independent constraints — like specifying an object's x-coordinate and y-coordinate separately. Bézout's identity gives us "basis vectors" that are 1 modulo one modulus and 0 modulo all the others, letting us build a solution directly as a linear combination.
Proof
We first prove the two-modulus case (k=2) and then extend by induction. Since gcd(m1,m2)=1, Bézout's identity gives integers u,v with m1u+m2v=1. Set x0=a1m2v+a2m1u.
Check modulo m1: from m1u+m2v=1 we have m2v=1−m1u≡1(modm1), while m1u≡0(modm1), so x0≡a1⋅1+a2⋅0=a1(modm1).
Symmetrically, modulo m2: m1u=1−m2v≡1(modm2) and m2v≡0(modm2), so x0≡a1⋅0+a2⋅1=a2(modm2). This proves existence for k=2.
For uniqueness when k=2: if x and x′ both solve the system, then x−x′≡0(modm1) and x−x′≡0(modm2), i.e. both m1 and m2 divide d=x−x′. Multiply the Bézout equation m1u+m2v=1 by d: d=dm1u+dm2v. Since m2∣d, the first term dm1u is a multiple of m1m2; since m1∣d, the second term dm2v is also a multiple of m1m2. Hence m1m2∣d, i.e. x≡x′(modm1m2).
For general k>2, induction: the first two congruences are equivalent (by the k=2 case) to a single congruence modulo m1m2; since m3 is coprime to both m1 and m2, it is coprime to their product m1m2, so we can combine again, and so on up to k. Notice also that the explicit sum x≡∑i=1kaiMiyi(modM) works directly for any k by the exact same reasoning: Mi=M/mi is coprime to mi so its inverse yi exists, and every term j=i contains mi as a factor of Mj. ■
When gcd(m1,m2)=1, the map ψ(xmodm1m2)=(xmodm1,xmodm2) is a ring isomorphism Z/(m1m2)Z≅(Z/m1Z)×(Z/m2Z), and restricting ψ to invertible elements proves φ(m1m2)=φ(m1)φ(m2).
Why is it true?
This is the structural meaning of CRT: arithmetic modulo m1m2 literally is two independent copies of modular arithmetic (one mod m1, one mod m2) running side by side. That is both why Euler's totient φ factors cleanly across coprime numbers, and why computers can speed up big-integer and cryptographic calculations by doing them in each small component separately.
Proof
First, ψ is well-defined: if x≡x′(modm1m2) then m1m2∣(x−x′), so both m1 and m2 divide x−x′, meaning x≡x′(modm1) and x≡x′(modm2).
Second, ψ respects addition and multiplication in each coordinate because reduction modulo mi does (proved in the first topic on congruences), so ψ is a ring homomorphism.
Third, Theorem 1 above says precisely that for every pair (a1,a2) there is an x mapped to it (so ψ is surjective) and that x is unique modulo m1m2 (so ψ is injective). Hence ψ is a bijection and therefore a ring isomorphism.
Finally, in a product ring R1×R2, an element (u1,u2) has a multiplicative inverse (v1,v2) iff u1v1=1 in R1 and u2v2=1 in R2 — that is, iff both coordinates are invertible. Since a ring isomorphism preserves invertibility, the invertible elements of Z/(m1m2)Z (of which there are φ(m1m2)) correspond bijectively to pairs of invertible elements in (Z/m1Z)×(Z/m2Z) (of which there are φ(m1)φ(m2)). Counting both sides gives φ(m1m2)=φ(m1)φ(m2). Combined with φ(pr)=pr−pr−1 for a prime power (where the non-coprime numbers are just the pr−1 multiples of p), this immediately yields the general product formula for φ(n) used in the previous topic. ■
AdvancedReal-World Applications and Worked Examples
Beyond classical puzzles, the Chinese remainder theorem is a workhorse of modern computation. Every production RSA implementation (OpenSSL, BoringSSL, hardware security modules) uses CRT-RSA (Quisquater–Couvreur, 1982) to compute cdmodpq by computing modulo p and modulo q separately and recombining via CRT — roughly a 4-times speedup because modular exponentiation costs O((logm)3) and halving the bit-length of the modulus cuts the work per exponentiation by 23=8 (done twice, so 8/2=4 times faster overall). The same idea underlies Residue Number Systems (RNS) in fast big-integer libraries and homomorphic encryption, as well as astronomical and calendar cycle calculations.
Example: Solving Sunzi's original soldier puzzle
Find all integers x satisfying x≡2(mod3), x≡3(mod5), and x≡2(mod7).
Solution
The moduli m1=3,m2=5,m3=7 are pairwise coprime with product M=3×5×7=105, so the explicit CRT formula applies with M1=105/3=35, M2=105/5=21, M3=105/7=15.
Find each inverse yi of Mi modulo mi: (1) 35≡2(mod3), and 2×2=4≡1(mod3), so y1=2; (2) 21≡1(mod5), so y2=1; (3) 15≡1(mod7), so y3=1.
Substitute into x≡∑i=1kaiMiyi(modM) with (a1,a2,a3)=(2,3,2): x≡2⋅35⋅2+3⋅21⋅1+2⋅15⋅1=140+63+30=233(mod105).
Since 233=2×105+23, reducing modulo 105 gives x≡23(mod105). Quick check: 23=3×7+2≡2(mod3), 23=5×4+3≡3(mod5), 23=7×3+2≡2(mod7). All three hold, and the smallest positive solution is **23**.
Example: Fast RSA decryption via CRT (CRT-RSA)
In the RSA example from the previous topic (p=5,q=11,n=55,d=27, ciphertext c=8), compute m=827mod55 by splitting it into two tiny calculations modulo 5 and modulo 11 instead of working modulo 55.
Solution
Modulo p=5: reduce the base 8≡3(mod5), and reduce the exponent d=27 modulo p−1=4 using Fermat's little theorem (27=4×6+3, so dp=3). Then mp=33=27≡2(mod5) — a single tiny cube!
Modulo q=11: the base is 8, and reduce the exponent 27 modulo q−1=10 using Fermat (27=10×2+7, so dq=7). Compute 87mod11: since 8≡−3(mod11), we have 82≡9≡−2, 84≡4, 87=84⋅82⋅8≡4⋅(−2)⋅(−3)=24≡2(mod11).
Now recombine m≡2(mod5) and m≡2(mod11) by CRT: since both remainders happen to be 2, the unique solution modulo 55 is immediately m≡2(mod55) (in general, with different mp,mq, one applies the two-modulus CRT formula once). Notice we never squared a number larger than 11 and never used an exponent larger than 7 — for 2048-bit RSA primes this same split cuts decryption time to roughly a quarter.
ResearchCRT at the research frontier: fast arithmetic, fault attacks, and lattice cryptography
Find the smallest non-negative integer x satisfying x≡1(mod3) and x≡2(mod5).
If m1=4,m2=9,m3=25, the solution to a CRT system with these moduli is unique modulo:
What can you say about the system x≡1(mod4) and x≡0(mod6)?
Why does CRT speed up RSA decryption cdmodpq by roughly 4 times?