← LibraryChinese Remainder Theorem AlgorithmsEngineering · MathematicsLesson 6/11← PrevNext →
GuidePublished 6 Aug 20264 min readBy Kevin JoginComputational Number TheoryFoundational AlgorithmsChinese Remainder TheoremCRT
Skip to the main content

MathematicsFoundational Algorithms

Chinese Remainder Theorem Algorithms

Reassembling a large result from small modular pieces — the technique that keeps intermediate coefficients from exploding.

Executive summary

Divide the problem across small moduli, then reassemble

Given pairwise coprime moduli, a system of congruences has a unique solution modulo their product. Constructively this converts one hard computation over large integers into many easy computations over small ones. The strategy — reduce, compute in parallel, reconstruct — is the standard defence against intermediate coefficient explosion in exact linear algebra, polynomial GCD and determinant computation.

Learning objectives

  • State the Chinese remainder theorem and its uniqueness condition.
  • Implement Garner's incremental reconstruction and explain its advantage.
  • Estimate how many primes are needed for a given result bound.
  • Recognise unlucky primes and handle them correctly.

Section 01Statement and construction

Let m1, …, mk be pairwise coprime and M their product. For any residues ri there is a unique x modulo M with x ≡ ri (mod mi) for all i.

x = ∑i ri Mi (Mi−1 mod mi)   mod M,    Mi = M/mi
Coprimality is not optional

If the moduli share a factor the system may be inconsistent, and the direct formula silently produces nonsense because the required inverse does not exist. Verify pairwise coprimality, or use distinct primes, which makes it automatic.

Section 02Garner's incremental algorithm

The direct formula requires the full product M in advance and does all its arithmetic at full width. Garner's algorithm instead builds the answer in mixed-radix form, performing each step in single precision and only widening at the end.

AlgorithmGarner's incremental CRTin: (ri, mi)  →  out: x mod ∏mi
  1. For i from 2 to k, precompute ci ← (m1·…·mi−1)−1 mod mi. Depends only on the moduli, so it is reusable.
  2. Set x ← r1 and P ← m1.
  3. For i from 2 to k:
  4.    Set t ← (ri − x) · ci mod mi. Single-precision arithmetic.
  5.    Set x ← x + t · P, then P ← P · mi.
  6. Return x, the unique solution modulo P.
Moduli can be added one at a time, so the computation can run until a size bound or a stability check is satisfied — no need to fix k in advance.

The incremental property is the practical advantage: a modular determinant computation can keep adding primes until two successive reconstructions agree, without knowing the answer's size beforehand.

Section 03The multi-modular strategy

  1. Stage 01Bound the resultDerive an a priori bound on the answer — Hadamard's bound for a determinant, Mignotte's bound for polynomial factors.
  2. Stage 02Choose primesSelect enough primes, usually of machine-word size, so their product exceeds twice the bound. Word-size primes keep every operation single-precision.
  3. Stage 03Compute in parallelSolve the problem independently modulo each prime. The subproblems are fully independent, so this parallelises perfectly.
  4. Stage 04ReconstructApply CRT, then map into the symmetric range (−M/2, M/2] if the answer may be negative, or apply rational reconstruction if it may be a fraction.
Unlucky primes

A prime is unlucky if the structure of the problem degenerates modulo it — a matrix that is invertible over ℚ becoming singular, or a polynomial losing degree because its leading coefficient vanishes. Unlucky primes are rare but not negligible. Detect them by a rank or degree check and discard the prime; do not attempt to repair the result.

ReferenceFrequently asked questions

How many primes are needed?

Enough that their product exceeds twice the bound on the largest coefficient — the factor of two accommodates negative values represented in the symmetric range. With 63-bit primes, each contributes about 19 decimal digits of the final result.

Can I use the same primes every time?

For repeated computations of similar size, yes, and caching the Garner constants is worthwhile. But if an adversary or a structured input family can predict the primes, degenerate cases become more likely, so randomised selection from a pool is safer for library code.

Is the residue number system worth using for general arithmetic?

Rarely. Addition and multiplication are trivially parallel in RNS, but comparison, division and sign detection are all awkward and expensive. RNS suits fixed pipelines of additions and multiplications, not general-purpose arithmetic.

NavigateContinue in this stream

Curated next steps from this page. The site also surfaces algorithmically related reading below.

ProvenanceSources and further reading

This page is an original KEVOS explanatory article. It presents the underlying mathematics — definitions, algorithms, complexity results and selection criteria — in KEVOS editorial voice. No text is reproduced from any copyrighted source. Where numerical tables are relevant, KEVOS links to live authoritative databases rather than republishing static values.

Page ID
KV-MATH-0006
Taxonomy
ENG-MATH — Engineering / Mathematics
Collection
COL-CANT-001
Topic stream
CANT-FOUNDATIONS
Version
1.1.0 / content 2026.08
Last reviewed
2026-08-06

Continue learning

The Extended Euclidean Algorithm and Modular InversesGuide · MathematicsNEXT LESSON →Continued Fraction ExpansionsGuide · MathematicsThe Euclidean Algorithm and GCD ComputationGuide · MathematicsLegendre, Jacobi and Kronecker SymbolsGuide · Mathematics