Intuition, Vocabulary and Foundations
A congruence describes a repeating constraint rather than one isolated integer. For example, $x\equiv2\pmod5$ means that $x$ may be $2,7,12,17$, or any integer differing from $2$ by a multiple of $5$. An everyday analogy is a repeating schedule: if a task occurs every five days and its next occurrence is two days after a reference date, every occurrence has the same residue modulo $5$.
A linear congruence has the form
$$ ax\equiv b\pmod m, \qquad m\ge1. $$
This is equivalent to $m\mid(ax-b)$ and therefore to the linear Diophantine equation $ax-my=b$. Let $d=\gcd(a,m)$. Bézout's identity shows that a solution exists exactly when $d\mid b$.
If $d\mid b$, division by $d$ gives
$$ \frac adx\equiv\frac bd\pmod{\frac md}. $$
The reduced coefficient is coprime to the reduced modulus, so it has an inverse. A reduced solution is
$$ x_0\equiv\frac bd\left(\frac ad\right)^{-1}\pmod{\frac md}. $$
Modulo the original modulus, there are exactly $d$ solutions:
$$ x=x_0+k\frac md, \qquad 0\le k\lt d. $$
For example, $12x\equiv18\pmod{30}$ has $d=6$. Reduction gives $2x\equiv3\pmod5$, hence $x\equiv4\pmod5$. Its six solutions modulo $30$ are $4,9,14,19,24,29$.
The Chinese Remainder Theorem combines residue constraints. For
$$ x\equiv r_1\pmod{m_1}, \qquad x\equiv r_2\pmod{m_2}, $$
let $g=\gcd(m_1,m_2)$. The constraints are compatible exactly when $g\mid(r_2-r_1)$. When compatible, their common solutions form one residue class modulo $\operatorname{lcm}(m_1,m_2)$. If the moduli are coprime, compatibility is automatic and the answer is unique modulo $m_1m_2$.
A linear congruence $ax\equiv b\pmod m$ is solvable exactly when $\gcd(a,m)\mid b$. Two residue constraints are compatible exactly when their residues agree modulo the gcd of their moduli; when they do, the merged answer is unique modulo the lcm.
| Concept | Example | Non-example or boundary | Assumption | Question answered |
|---|---|---|---|---|
| Linear congruence | $3x\equiv6\pmod9$ | $6x\equiv5\pmod9$ has no solution | Positive modulus | Which residue classes satisfy a modular linear equation? |
| Unique reduced solution | $3x\equiv4\pmod7$ gives $x\equiv6\pmod7$ | Uniqueness may fail before dividing out a nontrivial gcd | $\gcd(a,m)=1$ | When may the coefficient be inverted? |
| Multiple solutions | $12x\equiv18\pmod{30}$ has six solutions | Reporting only $x\equiv4\pmod{30}$ misses five solutions | $\gcd(a,m)\mid b$ | How many solutions exist modulo $m$? |
| Classical CRT | $x\equiv2\pmod3$ and $x\equiv3\pmod5$ | Compatibility is automatic only because the moduli are coprime | Pairwise-coprime moduli | Is there one answer modulo the product? |
| Generalized CRT | $x\equiv4\pmod6$ and $x\equiv10\pmod{14}$ | Replacing $10$ by $9$ makes the system inconsistent | Residues agree modulo the gcd | Can non-coprime constraints be merged? |
| Canonical result | $x\equiv23\pmod{105}$ | $128$ is equivalent but not canonical | Normalize into $[0,M)$ | Which representative should be returned? |
| Modulus one | $x\equiv0\pmod1$ adds no restriction | Inversion modulo $1$ is not meaningful | Treat the one-class system explicitly | How should an identity constraint behave? |
Step-by-Step Derivations and Algorithms
To solve $ax\equiv b\pmod m$, normalize $a$ and $b$ modulo $m$, then compute $d=\gcd(a,m)$. If $d\nmid b$, no solution exists because every integer combination of $a$ and $m$ is divisible by $d$.
When $d\mid b$, define
$$ a'=\frac ad, \qquad b'=\frac bd, \qquad m'=\frac md. $$
Since $\gcd(a',m')=1$, extended Euclid supplies $(a')^{-1}$ modulo $m'$, and
$$ x_0\equiv b'(a')^{-1}\pmod{m'}. $$
The compact result $(x_0,m',d)$ represents the $d$ solutions modulo $m$: begin at $x_0$, advance by $m'$, and stop after $d$ residues. This avoids allocating a large list when $d$ is large.
To merge two congruences, write $x=r_1+m_1k$ and substitute into the second one:
$$ m_1k\equiv r_2-r_1\pmod{m_2}. $$
Extended Euclid produces $s,t$ satisfying $sm_1+tm_2=g$, where $g=\gcd(m_1,m_2)$. If $g\nmid(r_2-r_1)$, the system is inconsistent. Otherwise,
$$ k\equiv\frac{r_2-r_1}{g}s\pmod{\frac{m_2}{g}}. $$
The merged residue is $r=r_1+m_1k$, normalized modulo
$$ M=\operatorname{lcm}(m_1,m_2)=m_1\frac{m_2}{g}. $$
Incremental CRT begins with the identity constraint $x\equiv0\pmod1$ and merges one constraint at a time. After every successful merge, $(r,M)$ represents exactly the integers satisfying all constraints processed so far.
flowchart TD
A["Normalize coefficients and residues"] --> B{"Single congruence?"}
B -->|Yes| C["Compute coefficient-modulus gcd"]
C --> D{"Does gcd divide target?"}
D -->|No| E["Report no solution"]
D -->|Yes| F["Reduce and invert coefficient"]
B -->|No| G["Start with residue 0 modulo 1"]
G --> H["Merge next constraint"]
H --> I{"Residues agree modulo gcd?"}
I -->|No| E
I -->|Yes| J["Update residue modulo lcm"]
J --> K{"More constraints?"}
K -->|Yes| H
K -->|No| L["Return canonical residue and period"]
Each CRT merge preserves all previously processed solutions and adds exactly one new congruence; compatibility is equivalent to divisibility of the residue difference by the moduli's gcd. A merge uses $O(\log\min(m_1,m_2))$ Euclidean steps and $O(1)$ auxiliary storage, although operand bit lengths grow with the accumulated lcm.
Implementation, Complexity and Engineering
A robust interface rejects nonpositive moduli, normalizes every residue, distinguishes inconsistency from a valid zero residue, and returns the combined modulus with the residue. For a linear congruence, returning a base, step, and count is more scalable than eagerly listing all solutions.
The expression $m_1(m_2/g)$ computes the lcm more safely than $(m_1m_2)/g$, but it can still exceed a fixed-width type. Production code should use checked multiplication, a wider integer type, or arbitrary-precision integers. The same concern applies to $m_1k$ while constructing a merged residue.
| Implementation choice | Complexity | Numeric boundary | Failure mode | Mitigation |
|---|---|---|---|---|
| Brute-force search | Up to $O(m)$ residue tests | Impractical for large $m$ | Treating enumeration as the algorithm | Use gcd reduction and one inverse |
| Reduced linear solver | $O(\log m)$ Euclidean steps | May represent many solutions | Returning only one residue modulo the original modulus | Return base, step, and count |
| Closed-form coprime CRT | About $O(k\log M)$ arithmetic steps | Product modulus grows rapidly | Assuming pairwise coprimality | Validate gcds or use generalized merging |
| Incremental generalized CRT | One gcd merge per constraint | Accumulated lcm may become large | Continuing after incompatibility | Stop when the difference fails the gcd test |
| Fixed-width lcm calculation | Constant machine operations when safe | $m_1m_2$ may overflow | Multiplying before dividing by the gcd | Divide first and use checked multiplication |
| Arbitrary-precision CRT | Operand-dependent big-integer cost | Memory grows with bit length | Reporting only operation counts | Measure bit sizes and actual latency |
| Eager solution listing | $O(d)$ output for $d=\gcd(a,m)$ | The solution set may be enormous | Allocating unnecessary residues | Retain the symbolic progression |
| Residue-only result | Small return value | Loses the solution period | Confusing equivalent systems | Return residue and combined modulus |
Applications, Case Studies and Exercises
Congruence systems model repeating schedules, synchronized counters, rotating identifiers, calendar calculations, sharded storage layouts, and reconstruction from smaller arithmetic channels. The compatibility test is also an engineering diagnostic: it distinguishes “no synchronized time exists” from “the search has not run long enough.”
A reliable oracle substitutes the returned residue into every original congruence. Useful properties include invariance under adding multiples of an input modulus, equivalence under reordered constraints, agreement with brute force on small systems, and a combined modulus equal to the lcm of compatible input moduli.
Apply Linear Congruences & Chinese Remainder Theorem
Case study 1 — three repeating schedules. Find the earliest nonnegative time satisfying
$$ x\equiv2\pmod3, \qquad x\equiv3\pmod5, \qquad x\equiv2\pmod7. $$
- Merge the first two constraints to obtain $x\equiv8\pmod{15}$.
- Substitute $x=8+15k$ into the third constraint.
- Solve $8+15k\equiv2\pmod7$, obtaining $k\equiv1\pmod7$.
- Obtain $x=23$ with combined period $105$.
- Verify all three original residues.
Success criterion: return $(23,105)$ and prove every solution is $23+105t$ for an integer $t$.
Case study 2 — synchronizing non-coprime services. Consider
$$ x\equiv4\pmod6, \qquad x\equiv10\pmod{14}. $$
- Compute $\gcd(6,14)=2$.
- Check that $10-4=6$ is divisible by $2$.
- Merge the constraints to obtain $x\equiv10\pmod{42}$.
- Verify both original residues.
- Replace $10$ by $9$ and explain why the new difference, $5$, makes the system inconsistent.
Success criterion: return $(10,42)$ for the compatible system and report inconsistency for the modified system without searching timestamps.
Hands-on trace. Solve $12x\equiv18\pmod{30}$.
- Compute $d=\gcd(12,30)=6$.
- Confirm $d\mid18$.
- Reduce to $2x\equiv3\pmod5$.
- Use $2^{-1}\equiv3\pmod5$ to obtain $x\equiv4\pmod5$.
- Generate and verify $4,9,14,19,24,29$ modulo $30$.
Tiered exercises
- Recall: State the solvability condition for $ax\equiv b\pmod m$.
- Classification: Decide whether $8x\equiv6\pmod{14}$ and $8x\equiv5\pmod{14}$ are solvable.
- Multiplicity: Find every solution of $9x\equiv12\pmod{15}$.
- Derivation: Prove the generalized CRT compatibility condition.
- Trace: Merge $x\equiv1\pmod4$ with $x\equiv7\pmod9$.
- Debugging: Explain why multiplying all moduli gives the wrong period when they share factors.
- Implementation: Add checked fixed-width multiplication for the combined lcm.
- Property testing: Shuffle compatible constraints and verify the canonical result is unchanged.
- Extension: Return the first solution in $[L,R]$ without enumerating earlier solutions.
- Challenge: Compare incremental generalized CRT with a product-based construction on large coprime moduli.
Solve one linear congruence by reducing with its gcd; merge many constraints with generalized CRT unless pairwise coprimality is guaranteed. Always normalize residues, retain the combined modulus, test compatibility before constructing a result, and guard lcm growth; these habits prepare the way for prime-field algorithms, discrete logarithms, and higher modular equations.
Next in the Series
In Primality Testing & Integer Factorization, we'll explore Test and factor large machine integers robustly..