Intuition, Vocabulary and Foundations
A congruence describes a repeating schedule. The statement $x\equiv a\pmod m$ means every valid value differs from $a$ by a multiple of $m$. Combining congruences resembles synchronizing clocks: their requested readings may align periodically, or a shared cycle may make them contradictory.
For example, $x\equiv2\pmod6$ and $x\equiv5\pmod9$ are compatible because $\gcd(6,9)=3$ divides $5-2=3$. Their solutions form one residue class modulo $\operatorname{lcm}(6,9)=18$, namely $x\equiv14\pmod{18}$. In contrast, $x\equiv2\pmod6$ and $x\equiv4\pmod9$ are inconsistent because $3\nmid2$.
The generalized Chinese Remainder Theorem permits non-coprime moduli. Two constraints are compatible exactly when their residues agree modulo the gcd; after a successful merge, the combined modulus is the lcm, not generally the product.
Garner’s algorithm addresses pairwise coprime moduli. It reconstructs a value through mixed-radix digits $x=c_0+c_1m_0+c_2m_0m_1+\cdots$, where $0\le c_i\lt m_i$. Each digit satisfies the next congruence without disturbing previous ones. The representation is unique below $\prod_i m_i$ and can be evaluated exactly or modulo a separate target.
Generalized CRT first checks whether residues agree modulo every shared gcd; only compatible constraints may be merged, and the resulting period is their lcm. Garner’s algorithm instead requires pairwise coprime moduli and replaces a potentially huge direct reconstruction with uniquely determined mixed-radix digits.
| Concept | Definition or invariant | Concrete example | Non-example or boundary | Question answered |
|---|---|---|---|---|
| Normalized congruence | $0\le a\lt m$ with $m\gt0$ | $-1\bmod5=4$ | A zero modulus is invalid | What canonical residue represents the class? |
| Compatibility | $\gcd(m,n)\mid(b-a)$ | $2\bmod6$ and $5\bmod9$ are compatible | $2\bmod6$ and $4\bmod9$ contradict | Can both constraints hold? |
| Combined period | $\operatorname{lcm}(m,n)$ | Moduli $6$ and $9$ merge to period $18$ | Product $54$ is not minimal | What is the unique solution modulus? |
| Coprime CRT | Pairwise gcds equal $1$ | Moduli $3,5,7$ | Moduli $6,10$ are not coprime | When is the period the full product? |
| Mixed-radix digit | $0\le c_i\lt m_i$ | For $3,5$, write $x=c_0+3c_1$ | Digits are not decimal digits | How is reconstruction built incrementally? |
| Exact reconstruction | Canonical $x$ below the product | Recover $23$ modulo $3,5,7$ | It may overflow a machine word | What is the complete representative? |
| Modular reconstruction | Compute $x\bmod M$ directly | Recover a coefficient modulo $10^9+7$ | It does not reveal exact $x$ | How can the full product be avoided? |
| Uniqueness | One class modulo the merged period | $14\bmod18$ represents all solutions | Infinitely many integer representatives remain | In what sense is reconstruction unique? |
Step-by-Step Derivations and Algorithms
To merge $x\equiv a\pmod m$ and $x\equiv b\pmod n$, write $x=a+mt$. Substitution gives $mt\equiv b-a\pmod n$. Let $g=\gcd(m,n)$. A solution exists exactly when $g\mid(b-a)$. Divide by $g$ to obtain
$$ \frac{m}{g}t\equiv\frac{b-a}{g}\pmod{\frac{n}{g}}. $$
The reduced coefficient $m/g$ is invertible modulo $n/g$, so solve for $t$, substitute into $a+mt$, and normalize modulo $m(n/g)=\operatorname{lcm}(m,n)$. Repeating this merge maintains that the current residue class equals the intersection of all processed constraints.
For pairwise coprime moduli, Garner assumes digits $c_0,\ldots,c_{i-1}$ are fixed. If $x_{i-1}$ is their partial mixed-radix value, the next digit satisfies
$$ x_{i-1}+c_i\prod_{j=0}^{i-1}m_j\equiv r_i\pmod{m_i}. $$
The accumulated product is invertible modulo $m_i$, so $c_i$ is unique. Once all digits are known, evaluate them with arbitrary-precision integers or reduce each accumulation step modulo a target.
flowchart TD
A["Normalize residues and moduli"] --> B{"Moduli pairwise coprime?"}
B -->|No or unknown| C["Merge constraints incrementally"]
C --> D["Compute shared gcd"]
D --> E{"Difference divisible by gcd?"}
E -->|No| F["Report contradiction"]
E -->|Yes| G["Solve reduced congruence"]
G --> H["Return residue modulo lcm"]
B -->|Yes| I["Compute mixed-radix digits"]
I --> J{"Desired output?"}
J -->|Exact| K["Evaluate full reconstruction"]
J -->|Modulo target| L["Evaluate digits modulo target"]
Incremental CRT preserves that the current class is exactly the intersection of all processed constraints; compatibility and the reduced inverse prove each merge. Merging $k$ constraints takes $O(k)$ gcd and inverse operations, while straightforward Garner takes $O(k^2)$ modular operations and $O(k)$ storage.
Implementation, Complexity and Engineering
The reference implementation separates generalized CRT from Garner reconstruction. merge_crt accepts non-coprime moduli and returns None on contradiction. Garner checks pairwise coprimality before producing digits, preventing a nonexistent inverse from being mistaken for an unrelated failure.
Python integers grow automatically. In fixed-width languages, compute lcm as (m / gcd) * n, check multiplication, and use overflow-safe modular multiplication. Modular Garner keeps accumulated products reduced by the target modulus, but digit construction still uses arithmetic modulo the original source moduli.
| Method or choice | Preconditions | Complexity | Numeric risk | Failure mode | Mitigation |
|---|---|---|---|---|---|
| Incremental generalized CRT | Positive moduli | $O(k)$ gcd and inverse operations | Merged lcm grows rapidly | Skipping compatibility | Check the difference modulo the gcd |
| Coprime product CRT | Pairwise coprime moduli | $O(k)$ inverses plus reconstruction | Large product and weighted terms | Using shared-factor moduli | Verify pairwise gcds or merge generally |
| Straightforward Garner | Pairwise coprime moduli | $O(k^2)$ time, $O(k)$ space | Exact evaluation products grow | Inverse does not exist | Validate the domain before digit construction |
| Garner modulo target | Pairwise coprime sources, positive target | $O(k^2)$ | Source arithmetic still needs safety | Confusing modular and exact output | Name the target and document information loss |
| Arbitrary precision | Valid algorithmic inputs | Operand-length-dependent | Allocation and multiplication cost | Assuming constant-time arithmetic | Include bit complexity |
| Fixed-width integers | All intermediates fit | Fast native operations | Silent lcm or product overflow | Correct residue with corrupted modulus | Divide first and use checked operations |
| Normalized residues | Positive modulus | $O(1)$ per constraint | Minimal | Comparing negative representatives | Normalize at the input boundary |
| Brute-force oracle | Small combined period | Up to the lcm | Impractical at scale | Deploying the oracle | Reserve it for differential tests |
Applications, Case Studies and Exercises
Generalized CRT suits schedules, counters, and constraints whose moduli may share factors. Garner is especially useful when a large integer or polynomial coefficient was computed modulo several machine-friendly primes and only a final modular value is required.
Apply Generalized CRT & Garner's Algorithm
Case study 1 — synchronize non-coprime schedules. Merge $x\equiv2\pmod6$ and $x\equiv5\pmod9$. Compute $g=3$, verify compatibility, solve the reduced congruence, and obtain $x\equiv14\pmod{18}$. Replace the second residue with $4$ and explain the contradiction. Success means every accepted result satisfies both inputs and every rejected pair disagrees modulo the gcd.
Case study 2 — reconstruct a large convolution coefficient. Suppose an NTT pipeline returns one coefficient modulo three pairwise coprime transform primes. Compute its Garner digits, evaluate the exact representative when a coefficient bound guarantees uniqueness, and separately evaluate it modulo an application modulus. Verify every source residue and record the source-modulus product.
Hands-on comparison. Generate one thousand seeded systems with two to six small moduli. Compare generalized CRT with brute force through the lcm. For pairwise coprime systems, compare generalized CRT, exact Garner, and modular Garner. Include negative residues, redundant constraints, modulus $1$ in generalized CRT, and deliberate contradictions. Success means all valid methods agree and invalid Garner domains are rejected.
Tiered exercises. Recall: state the gcd compatibility condition. Derive: prove the merged modulus is the lcm. Implement: return the first solution inside a closed interval. Debug: expose overflow from computing $mn/g$ instead of $(m/g)n$. Extend: precompute Garner inverses for repeated reconstructions with a fixed modulus set.
Use incremental generalized CRT when moduli may share factors or contradictions require diagnosis; use Garner when source moduli are pairwise coprime and mixed-radix or target-modular reconstruction avoids unwieldy products. Always distinguish a canonical residue class, an exact representative justified by an external size bound, and a value reconstructed only modulo another modulus.