How the Chinese remainder theorem builds a residue

Short answer: The Chinese remainder theorem says that if the moduli m1, …, mk are pairwise coprime, then for any residues a1, …, ak there is one solution modulo M = m1·…·mk of the system x ≡ ai (mod mi). The constructive form computes, for each i, the product Mi = M/mi and the inverse of Mi modulo mi, then sums ai·Mi·yi. The solution is unique modulo M. Pairwise coprime is the hypothesis. One modulus that shares a factor with another makes the system dependent.


What the problem actually asks

A set of independent modular constraints determines a unique residue class modulo the product, provided no two moduli share a prime factor. The construction is explicit. Mi is 0 modulo every mj except mi, so multiplying by the inverse of Mi modulo mi turns Mi into a term that is 1 modulo mi and 0 modulo the others. The sum of ai times those terms hits every residue at once.

If gcd(mi, mj) does not divide ai − aj, the system has no solution. If it does divide, there may still be a solution modulo a smaller product. The pairwise-coprime construction does not cover that case. A successive-merge version that uses the extended gcd at each step does.


What the algorithm guarantees

Under pairwise coprimality the displayed sum is a solution, and any two solutions differ by a multiple of M. Time is k inverses and O(k) multiplications of integers the size of M, plus the cost of computing M. Extra memory is the Mi and the inverses. The inverse exists because gcd(Mi, mi) = 1, which is the pairwise hypothesis applied to the product of the other moduli.

The theorem is not a statement about prime moduli only. Any pairwise coprime integers work, including composite ones that happen to share no factors. Two even moduli are not allowed in the basic statement.


The working steps

  1. Verify that gcd(mi, mj) = 1 for i ≠ j. If not, switch to a merge algorithm or report that the basic theorem does not apply.
  2. Compute M as the product of the moduli, and Mi = M/mi for each i.
  3. Compute yi, the inverse of Mi modulo mi, with the extended Euclidean algorithm.
  4. Set x = sum over i of ai·Mi·yi. Reduce x modulo M into the range the caller wants.
  5. Check x ≡ ai (mod mi) for each i. A mismatch means an inverse or a product was taken modulo the wrong integer.

Uniqueness is modulo M, not a unique integer. The construction needs inverses modulo each mi, not modulo M. A system with moduli that are not coprime can still be solvable and needs the extended-gcd merge, which also returns the obstruction when the differences of residues are not divisible by the gcds.


Approaches that look simpler and fail

Approach Why it fails when the moduli share a prime factor
Invert Mi modulo mi when a prime divides both The inverse does not exist. The product formula cannot be evaluated, and forcing a pseudo-inverse returns a number that misses some equations.
Return x without reducing modulo M and call it the only solution x + M is the same class. The theorem’s uniqueness is the class.
Multiply the congruences instead of mixing them with Mi A product of residues is a different arithmetic object. The explicit solution is the weighted sum.
Use the formula on a solvable system of non-coprime moduli The formula’s hypothesis is false. A successive merge may still find the solution, and this sum is not that algorithm.

Each inverse is the extended Euclidean algorithm. The theorem is the way RSA decrypts modulo p and modulo q and combines the results, and the way many counting problems reduce a composite modulus. The merge for non-coprime moduli is the extended-gcd version of the same idea.


A working example

The Bash script and the Python script build one residue for x≡2 (mod 3), x≡3 (mod 5), x≡2 (mod 7). Both print 23.

# mods 3,5,7 rems 2,3,2. Inverse of Mi modulo m by extended gcd, iterative.
mods=(3 5 7)
rems=(2 3 2)
M=105
x=0
for ((i=0; i<3; i++)); do
  m=${mods[i]}
  Mi=$((M / m))
  a=$Mi
  b=$m
  s0=1; s1=0
  while (( b != 0 )); do
    q=$((a / b))
    r=$((a % b))
    a=$b; b=$r
    ns=$((s0 - q * s1)); s0=$s1; s1=$ns
  done
  inv=$(( (s0 % m + m) % m ))
  x=$((x + rems[i] * Mi * inv))
done
echo $((x % M))
def egcd(a, b):
    if b == 0:
        return 1, 0
    x1, y1 = egcd(b, a % b)
    return y1, x1 - (a // b) * y1

mods = [3, 5, 7]
rems = [2, 3, 2]
M = 1
for m in mods:
    M *= m
x = 0
for a, m in zip(rems, mods):
    Mi = M // m
    inv, _ = egcd(Mi, m)
    x += a * Mi * (inv % m)
x %= M
assert x % 3 == 2 and x % 5 == 3 and x % 7 == 2
print(x)

Where it is used

The Chinese remainder theorem is how RSA-CRT decrypts modulo p and modulo q and combines the halves, which is the private operation in OpenSSL. It also reconstructs an integer from coprime residues. The public RSA operation does not need the split.


When you can drop this algorithm

Drop the product formula when the moduli may share factors. Merge with the extended gcd and let it reject inconsistent residues. Keep the explicit sum when the moduli are known to be pairwise coprime.


Why this stays published

We publish this so a search for the Chinese remainder theorem returns the explicit sum ai·Mi·yi and the pairwise-coprime hypothesis. The Chinese remainder theorem builds the unique solution modulo the product of pairwise coprime moduli as a sum of residues weighted by modular inverses.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *