Folioby Interconnected
Log InSign Up

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.

FO
Folio Official
March 1, 2026

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

Example 1 (From the Sunzi Suanjing).
A certain number leaves remainder 2 when divided by 3, remainder 3 when divided by 5, and remainder 2 when divided by 7. What is the number?

In other words, solve the system
x≡2(mod3),x≡3(mod5),x≡2(mod7).
The answer is x≡23(mod105) (where 105=3×5×7).

2 The theorem

Theorem 2 (Chinese Remainder Theorem).
Let m1​,m2​,…,mk​ be pairwise coprime (that is, gcd(mi​,mj​)=1 for i=j). Then the system
x≡a1​(modm1​),x≡a2​(modm2​),…,x≡ak​(modmk​)
has a unique solution modulo M=m1​m2​⋯mk​.
Remark 3.
In one sentence, the CRT says: the remainders modulo pairwise coprime integers carry exactly the same information as the single remainder modulo their product.

3 A constructive proof

Proof.
Set M=m1​m2​⋯mk​ and Mi​=M/mi​. Since Mi​ is the product of all moduli except mi​, and the moduli are pairwise coprime, gcd(Mi​,mi​)=1. By the extended Euclidean algorithm, there exists ti​ with Mi​ti​≡1(modmi​).

Define
x=i=1∑k​ai​Mi​ti​.
For each i: when j=i, mi​∣Mj​, so Mj​tj​≡0(modmi​). Meanwhile Mi​ti​≡1(modmi​). Therefore
x≡ai​⋅1+j=i∑​aj​⋅0=ai​(modmi​).

For uniqueness: if x and x′ are both solutions, then mi​∣(x−x′) for every i. Since the mi​ are pairwise coprime, M∣(x−x′), so x≡x′(modM). □
Example 4 (Solving Master Sun's problem).
m1​=3, m2​=5, m3​=7; M=105.
  • M1​=35;35t1​≡1(mod3) gives t1​=2 (since 35×2=70≡1(mod3)).

  • M2​=21;21t2​≡1(mod5) gives t2​=1 (since 21≡1(mod5)).

  • M3​=15;15t3​≡1(mod7) gives t3​=1 (since 15≡1(mod7)).

x=2×35×2+3×21×1+2×15×1=140+63+30=233≡23(mod105).

4 The ring-theoretic meaning: a direct product decomposition

The CRT has a clean algebraic formulation.

Theorem 5 (Ring isomorphism).
If gcd(m,n)=1, there is a ring isomorphism
Z/mnZ≅Z/mZ×Z/nZ,
given by xmodmn↦(xmodm,xmodn).
Remark 6.
This isomorphism says that "the remainder modulo mn" and "the pair of remainders modulo m and modulo n" carry exactly the same information. The mn possible remainders on the left correspond one-to-one with the m×n pairs of remainders on the right.

This is the number-theoretic incarnation of divide and conquer: break a computation modulo the large number mn into two smaller computations modulo m and modulo n, then combine the results.

5 Garner's algorithm

The direct formula from the constructive proof involves the quantities Mi​, which can be astronomically large and prone to overflow. Garner's algorithm avoids this problem.

Remark 7 (Garner's idea).
Represent x in a mixed-radix form:
x=r1​+r2​m1​+r3​m1​m2​+⋯+rk​m1​m2​⋯mk−1​.
Determine the coefficients r1​,r2​,…,rk​ one at a time:
  • From x≡a1​(modm1​): r1​=a1​modm1​.

  • Substituting x=r1​+r2​m1​+⋯ into x≡a2​(modm2​) gives r2​m1​≡a2​−r1​(modm2​), so r2​≡(a2​−r1​)m1−1​(modm2​).

  • Each subsequent ri​ is computed modulo mi​ in the same fashion.

Because every step operates modulo mi​, the intermediate values remain small.

6 Applications in competitive programming

Example 8 (Combining results from different prime moduli).
Suppose we want (kn​)modm where m=p1​p2​ is not prime. Compute (kn​)modp1​ and (kn​)modp2​ separately, then merge them via CRT to obtain the answer modulo m.
Remark 9 (Typical patterns).
The CRT is applicable in the following common scenarios:
  • 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, Z/mnZ≅Z/mZ×Z/nZ– 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.

Number TheoryMathematicsBetween the Lines
FO
Folio Official

Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.

1 followers·107 articles
Number Theory — Between the LinesPart 5 of 8
Previous
Fermat's little theorem and the hidden symmetry of remainders
Next
Quadratic residues: why exactly half the nonzero elements mod p are squares

Share your expertise with the world

Write articles with LaTeX support, build your audience, and earn from your knowledge.

Start Writing — It's Free

More from Folio Official

Folio Official·March 1, 2026

Why primes are the atoms of the integers: what the Fundamental Theorem of Arithmetic really says

Unique prime factorization is not as obvious as it seems. Through a counterexample in Z[sqrt(-5)], we witness uniqueness break down, then prove both the existence and uniqueness parts of the Fundamental Theorem, and survey the Sieve of Eratosthenes and the linear sieve.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

What does it mean to "divide evenly"? Why number theory begins with mod

The modular reduction map is a projection that deliberately discards information. Clocks, days of the week, and the competitive-programming modulus 10^9+7 all share the same algebraic structure. From the definition of divisibility through congruences to the ring Z/nZ, we uncover the reason number theory starts where it does.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

Why the Euclidean algorithm finds the greatest common divisor: from subtraction to division

The recursion gcd(a,b) = gcd(b, a mod b) hides an invariant: the set of common divisors never changes. Starting from a naive subtraction-based method and upgrading to division, we derive Bezout's identity, the extended Euclidean algorithm, and modular inverses.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

Number theory meets cryptography: why RSA works and what makes it secure

Every theorem in this series -- Fermat, Euler, the extended Euclidean algorithm, CRT -- corresponds to a component of the RSA cryptosystem. We trace each connection, work through a concrete example, and discuss the factoring barrier that underpins RSA's security.

Number TheoryMathematicsBetween the Lines