MathCompass

Chinese Remainder Theorem

Number Theory Expert

πŸ“‹ Prerequisites

The Chinese Remainder Theorem (CRT) solves systems of simultaneous congruences. "Find a number that leaves remainder 2 when divided by 3, remainder 3 when divided by 5, and remainder 2 when divided by 7." This is the classic Sun Zi problem from ancient China. CRT tells us when solutions exist and how to find them.

πŸ“š Key Concepts

The Chinese Remainder Theorem (Standard Form)

If $ m_1, m_2, ..., m_k $ are pairwise coprime (gcd of any two is 1), then for any integers $ a_1, a_2, ..., a_k $, the system:

\[ \begin{cases} x \equiv a_1 \pmod{m_1} \\ x \equiv a_2 \pmod{m_2} \\ \quad \vdots \\ x \equiv a_k \pmod{m_k} \end{cases} \]

has a unique solution modulo $ M = m_1 m_2 ... m_k $.

Two key takeaways:

Constructive Proof / Formula

Let $ M = m_1 m_2 ... m_k $ and $ M_i = M / m_i $ (product of all moduli except $m_i$).

Since moduli are pairwise coprime, gcd($M_i, m_i$) = 1, so $M_i$ has an inverse mod $m_i$. Call it $y_i$.

Then the solution is:

\[ x \equiv a_1 M_1 y_1 + a_2 M_2 y_2 + ... + a_k M_k y_k \pmod{M} \]

Why it works: each term $a_i M_i y_i$ is congruent to $a_i$ mod $m_i$ (since $M_i y_i \equiv 1$ mod $m_i$), and congruent to 0 mod every other $m_j$ (since $M_i$ contains $m_j$ as a factor).

Successive Substitution Method

Often easier for small systems. Solve two congruences at a time:

  1. \(Start with first congruence: write x = a_{1} + m_{1} \\cdot t\)
  2. \(Substitute into second congruence: a_{1} + m_{1} \\cdot t \\equiv a_{2} (mod m_{2})\)
  3. \(Solve for t: m_{1} \\cdot t \\equiv a_{2} - a_{1} (mod m_{2})\)
  4. Find t, then write new combined congruence
  5. Repeat with next congruence

This works even when moduli aren't coprime (as long as a solution exists).

Non-Coprime Moduli

When moduli aren't pairwise coprime, a solution exists iff for every pair i, j:

\[ a_i \equiv a_j \pmod{\gcd(m_i, m_j)} \]

If the congruences are consistent on the overlapping parts, there's a unique solution mod \(lcm(m_{1}, m_{2},\) ..., m_k).

\(If inconsistent (e.g., x \\equiv 1 mod 2 and x \\equiv 2 mod 4), no solution exists.\)

CRT as a Ring Isomorphism (for context)

CRT means $ \mathbb{Z}/M\mathbb{Z} \cong \mathbb{Z}/m_1\mathbb{Z} \times ... \times \mathbb{Z}/m_k\mathbb{Z} $ \(when the m_{i} are pairwise coprime.\)

\(In plain language: working mod M is equivalent to working mod each m_{i} separately, component by component.\)

This is why multiplicative functions factor over prime powers β€” because the ring splits into independent pieces.

Applications

How to Approach CRT Problems

  1. Check consistency: \(For non-coprime moduli, verify a_{i} \(\\equiv a\)_{j} mod gcd(m_{i}, m_{j})\)
  2. Choose method:
    • \(2 congruences, small numbers \\to successive substitution (easiest)\)
    • \(3+ congruences, coprime moduli \\to CRT formula\)
    • \(Non-coprime \\to successive substitution, checking consistency at each step\)
  3. Verify: Plug your answer back into every original congruence
⚠️ Common Mistake: The standard CRT formula only works when moduli are PAIRWISE COPRIME. If moduli share factors, you can't just apply the formula β€” \(first check if the system is consistent (a_{i} \(\\equiv a\)_{j} mod gcd(m_{i}, m_{j})).\) \(\(If they're inconsistent, there's no solution. Also: when computing M_{i} = M/m_{i}, make sure you divide by m_{i}, n\)ot\) \(\(multiply. And don't forget to find y_{i} = inverse of M_{i} mod m_{\)i} β€” \(it's M_{i} that needs the inverse, not m_{i\)}.\)
πŸ’‘ Key Insight: CRT lets you decompose hard modular problems into easy ones. Need to compute something mod 100? \(Compute it mod 4 and mod 25 separately (since 100 = 4 \\times 25 and gcd(4,25)=1), then combine with CRT.\) This "divide and conquer" strategy is everywhere in number theory. For competition problems with 2-3 congruences, successive substitution is usually faster than the formula β€” \(write x = a + m \\cdot t, substitute, solve for t, repeat.\)

✏️ Example Problems

πŸ“ Example 1 (Classic Sun Zi problem)

Find the smallest positive integer x such that:
\(x \\equiv 2 (mod 3)\)
\(x \\equiv 3 (mod 5)\)
\(x \\equiv 2 (mod 7)\)

3, 5, 7 are pairwise coprime. Use successive substitution.

Step 1: From first congruence: x = 2 + 3t

Step 2: \(Substitute into second: 2 + 3t \\equiv 3 (mod 5)\)

\(3t \\equiv 1 (mod 5)\)

\(Inverse of 3 mod 5 is 2 (since 3 \\times 2 = 6 \\equiv 1 mod 5)\)

\(t \\equiv 2 \\times 1 = 2 (mod 5)\)

t = 2 + 5s

Step 3: x = 2 + 3(2 + 5s) = 2 + 6 + 15s = 8 + 15s

\(Combined so far: x \\equiv 8 (mod 15)\)

Step 4: \(Substitute into third: 8 + 15s \\equiv 2 (mod 7)\)

8 mod 7 = 1, 15 mod 7 = 1

\(1 + s \\equiv 2 (mod 7)\)

\(s \\equiv 1 (mod 7)\)

s = 1 + 7u

Step 5: x = 8 + 15(1 + 7u) = 8 + 15 + 105u = 23 + 105u

Smallest positive: x = 23

Verify: 23 mod 3 = 2 βœ“, 23 mod 5 = 3 βœ“, 23 mod 7 = 2 βœ“

Answer: 23

πŸ“ Example 2 (CRT formula method)

Use the CRT formula to solve:
\(x \\equiv 1 (mod 4)\)
\(x \\equiv 2 (mod 5)\)
Find the smallest positive solution.

\(m_{1} = 4, m_{2}\) = 5. gcd(4,5) = 1. CRT applies.

Step 1: \(M = 4 \\times 5 = 20\)

Step 2: \(M_{1} = M/m_{1} = 20/4 = 5, M_{2} = M/m_{2}\) = 20/5 = 4

Step 3: Find inverses \(y_{1} and y_{2}.\)

\(y_{1}\) = inverse of \(M_{1} = 5 mod m_{1}\) = 4

5 mod 4 = 1, inverse of 1 mod 4 is 1. So \(y_{1}\) = 1.

\(y_{2}\) = inverse of \(M_{2} = 4 mod m_{2}\) = 5

\(4 \\times 4 = 16 \\equiv 1 mod 5. So y_{2} = 4.\)

Step 4: Apply formula.

\[ x \equiv a_1 M_1 y_1 + a_2 M_2 y_2 \pmod{20} \]

\(x \\equiv 1 \\times 5 \\times 1 + 2 \\times 4 \\times 4 (mod 20)\)

\(x \\equiv 5 + 32 = 37 (mod 20)\)

37 mod 20 = 17

Step 5: Smallest positive x = 17

Verify: 17 mod 4 = 1 βœ“, 17 mod 5 = 2 βœ“

Answer: 17

πŸ“ Example 3 (Non-coprime β€” inconsistent)

Does the following system have a solution? Answer 1 for yes, 0 for no.
\(x \\equiv 1 (mod 4)\)
\(x \\equiv 2 (mod 6)\)

Moduli 4 and 6 are NOT coprime. gcd(4, 6) = 2.

Consistency check: \(We need a_{1} \\equiv a_{2} mod gcd(m_{1}, m_{2})\)

\(Check: is 1 \\equiv 2 (mod 2)\)?

1 mod 2 = 1

2 mod 2 = 0

\(1 \\neq 0.\) Inconsistent!

Intuition: \(x \\equiv 1 mod 4 means x is odd (1, 5, 9, 13, ...).\)

\(x \\equiv 2 mod 6 means x is even (2, 8, 14, 20, ...).\)

A number can't be both odd and even. No solution exists.

Answer: 0 (no solution)

\(Note: if the second congruence were x \\equiv 3 mod 6, then 1 \\equiv 3 mod 2 (both odd), so it would be consistent\) and we could find a solution.

Previous
End of Track