Chinese Remainder Theorem

Download as PDF

Ever faced a puzzle that felt like juggling multiple clues at once? The Chinese Remainder Theorem is a powerful mathematical tool for solving exactly that kind of problem. It helps you find a single number that satisfies several different remainder conditions simultaneously, a skill with surprising applications.

Chinese Remainder Theorem — an original Algebra911 reference diagram defining chinese remainder theorem with its key formula and a worked example.
A Beginner's Guide to the Chinese Remainder Theorem

What Is the Chinese Remainder Theorem?

The Chinese Remainder Theorem (CRT) is a principle from number theory that provides a way to find a unique solution to a system of simultaneous congruences, as long as their moduli are pairwise coprime. In simple terms, it's a method for finding a number that leaves specific remainders when divided by several different numbers.

Imagine you have a big bag of marbles. You don't know the total, but you know a few clues:

  • When you group the marbles into sets of 3, you have 2 left over.
  • When you group them into sets of 5, you have 3 left over.
  • When you group them into sets of 7, you have 2 left over.

How can you find the smallest possible number of marbles in the bag? This is exactly the kind of puzzle the Chinese Remainder Theorem was designed to solve. It takes multiple 'remainder' clues and combines them to find the single number that makes them all true.

What Are Congruences? A Quick Refresher

Before we can use the theorem, we need to be comfortable with the language it uses: congruences and modular arithmetic. You can think of modular arithmetic as "clock arithmetic." On a 12-hour clock, 15 hours after midnight is 3 o'clock. We can say that 15 is congruent to 3, modulo 12.

The notation we use is:

ab(modn)

This means that a and b have the same remainder when divided by n. For example, 172(mod5) is a true statement because both 17 and 2 leave a remainder of 2 when divided by 5.

The Chinese Remainder Theorem solves a system of these congruences. A typical problem looks like this, where you need to find the unknown number x:

xa1(modn1) xa2(modn2) xak(modnk)

There's one very important condition for the standard theorem to work: the moduli (the numbers you're dividing by, n1,n2,,nk) must be pairwise coprime. This just means that if you pick any two moduli from the list, their greatest common divisor (GCD) is 1. For example, 3, 5, and 7 are pairwise coprime because GCD(3,5)=1, GCD(3,7)=1, and GCD(5,7)=1.

How Does the Theorem Work with a Simple Case?

Let's build our intuition with a basic two-line problem. We want to find a number x that satisfies both of these conditions:

x2(mod3) x1(mod4)

Since the numbers are small, we can just list out possibilities for each line and see where they overlap.

  • Numbers that satisfy x2(mod3): 2, 5, 8, 11, 14, 17, ... (these are all 2 more than a multiple of 3).
  • Numbers that satisfy x1(mod4): 1, 5, 9, 13, 17, 21, ... (these are all 1 more than a multiple of 4).

Looking at our lists, we can see that 5 is the first number to appear in both. So, x=5 is our smallest positive solution. The next number to appear in both is 17. Notice that 17=5+12. This isn't a coincidence! The solutions repeat every 3×4=12 numbers. This is the core idea of the theorem: there is a unique solution modulo the product of the moduli.

Example 1

Find the smallest positive integer x that satisfies the system:

x2(mod3) x1(mod4)

Solution by listing:

  1. List numbers for the first congruence: The numbers that leave a remainder of 2 when divided by 3 are 2, 5, 8, 11, ...
  2. Check these numbers against the second congruence:
    • Does 2 work? 2÷4 has a remainder of 2. No, we need a remainder of 1.
    • Does 5 work? 5÷4 has a remainder of 1. Yes!

The smallest positive integer solution is x=5.

This listing method is great for simple problems, but it quickly becomes impractical for larger numbers. For that, we need a systematic algorithm.

What Are the Steps to Solve Any System of Congruences?

Here is a reliable, step-by-step method to solve any system of congruences that meets the coprime condition. Let's use the general form from before:

xa1(modn1) xa2(modn2) xak(modnk)
  1. Step 1: Check the Condition. Verify that all the moduli (n1,n2,,nk) are pairwise coprime. If they aren't, this method won't work.
  2. Step 2: Calculate the Big Modulus (N). Multiply all the individual moduli together to get one large product, N. N=n1×n2××nk This N is the number after which the solutions will repeat.
  3. Step 3: Calculate Partial Products (N_i). For each congruence i, calculate Ni=N/ni. In other words, Ni is the product of all moduli except for ni.
  4. Step 4: Find the Modular Inverses (y_i). This is the most clever step. For each i, you need to find a number yi that solves the congruence: Niyi1(modni) This yi is called the modular multiplicative inverse of Ni modulo ni. For small numbers, you can find this by testing values for yi: 1, 2, 3, ... until you find one that works.
  5. Step 5: Assemble the Solution (x_0). Now, you combine all the pieces you've found using one main formula. You multiply each remainder (ai), its partial product (Ni), and its inverse (yi) together, and then sum up all these results.
    x0=a1N1y1+a2N2y2++akNkyk
  6. Step 6: Find the Smallest Positive Solution. The number x0 from Step 5 is a valid solution, but it might be very large. The general solution is any number congruent to x0 modulo N. To find the smallest positive solution, you simply calculate the remainder of x0 when divided by N. x=x0(modN) This final remainder is your answer.

A Fully Worked Example: Putting the Steps into Practice

Let's solve the marble problem from the introduction. We need to find the smallest positive integer x such that:

x2(mod3) x3(mod5) x2(mod7)
Example 2

Solve the system of congruences above using the 6-step method.

Solution:

Step 1: Check the Condition. Our moduli are 3, 5, and 7. Since these are all prime numbers, they are automatically pairwise coprime. Check: GCD(3,5)=1, GCD(3,7)=1, GCD(5,7)=1. We can proceed.

Step 2: Calculate N. N=3×5×7=105. The solutions repeat every 105.

Step 3: Calculate N_i.

  • For n1=3: N1=N/n1=105/3=35.
  • For n2=5: N2=N/n2=105/5=21.
  • For n3=7: N3=N/n3=105/7=15.

Step 4: Find Inverses y_i.

  • For y1: We need to solve 35y11(mod3). First, simplify 35(mod3). 35=11×3+2, so the remainder is 2. The congruence becomes 2y11(mod3). Let's test values: If y1=1, we get 2. If y1=2, we get 4, and 41(mod3). Perfect! So, y1=2.
  • For y2: We need 21y21(mod5). Simplify 21(mod5). The remainder is 1. The congruence is 1y21(mod5). This is easy: y2=1.
  • For y3: We need 15y31(mod7). Simplify 15(mod7). The remainder is 1. The congruence is 1y31(mod7). So, y3=1.

Let's organize our findings in a table:

iainiNiyi
123352
235211
327151

Step 5: Assemble the Solution. Using the formula x0=a1N1y1+a2N2y2+a3N3y3: x0=(2)(35)(2)+(3)(21)(1)+(2)(15)(1) x0=140+63+30 x0=233

Step 6: Find the Smallest Positive Solution. We find the remainder of x0 when divided by N. x=233(mod105) Since 233=2×105+23, the remainder is 23.

The smallest number of marbles in the bag is 23.

Key formulas for chinese remainder theorem by Algebra911.
Key formulas for chinese remainder theorem by Algebra911.

How to Solve an Ancient Word Problem with the CRT

The theorem gets its name from early appearances in Chinese mathematical texts. Here is a classic problem, paraphrased from the work of the mathematician Sun Tzu.

Example 3

An army has an unknown number of soldiers. When the general arranges them in columns of 3, there are 2 soldiers left over. When arranged in columns of 5, there are 3 left over. When arranged in columns of 7, there are 2 left over. What is the smallest possible number of soldiers in the army?

Wait, this is the same problem as the marbles! Yes, it is. The underlying math is identical. Let's try a different one.

New Problem: Find the smallest positive integer that leaves a remainder of 1 when divided by 2, 3, and 5, but is perfectly divisible by 7.

Solution:

First, translate the word problem into a system of congruences:

x1(mod2) x1(mod3) x1(mod5) x0(mod7)

Step 1: Check Condition. The moduli are 2, 3, 5, and 7. All are prime numbers, so they are pairwise coprime.

Step 2: Calculate N. N=2×3×5×7=210.

Step 3: Calculate N_i.

  • N1=210/2=105
  • N2=210/3=70
  • N3=210/5=42
  • N4=210/7=30

Step 4: Find Inverses y_i.

  • 105y_1 \equiv 1 \pmod{2} \implies 1y_1 \equiv 1 \pmod{2} \implies y_1=1
  • 70y_2 \equiv 1 \pmod{3} \implies 1y_2 \equiv 1 \pmod{3} \implies y_2=1
  • 42y_3 \equiv 1 \pmod{5} \implies 2y_3 \equiv 1 \pmod{5} \implies y_3=3 (since 2×3=61)
  • 30y_4 \equiv 1 \pmod{7} \implies 2y_4 \equiv 1 \pmod{7} \implies y_4=4 (since 2×4=81)

Step 5: Assemble the Solution. x0=a1N1y1+a2N2y2+a3N3y3+a4N4y4 x0=(1)(105)(1)+(1)(70)(1)+(1)(42)(3)+(0)(30)(4) x0=105+70+126+0 x0=301

Step 6: Find the Smallest Positive Solution. x=301(mod210) Since 301=1×210+91, the remainder is 91.

The smallest such integer is 91. Let's check: 91 divided by 2, 3, or 5 leaves a remainder of 1. And 91=13×7, so it's perfectly divisible by 7.

What Are Common Mistakes to Avoid?

The Chinese Remainder Theorem is a powerful algorithm, but it has several steps where it's easy to make a small error. Here are the most common pitfalls to watch out for:

  • Forgetting the Coprime Check. The very first step is crucial. If you try to apply this method when the moduli are not pairwise coprime (e.g., trying to solve a system with (mod6) and (mod4)), you will not get a correct answer. A solution might not even exist.
  • Mixing Up ni and Ni. Remember, ni is the original modulus from the problem statement. Ni is the value you calculate by dividing the total product N by ni. Keep your work organized in a table to avoid confusion.
  • Errors Finding the Inverse. Finding yi such that Niyi1(modni) can be tricky. Always double-check your inverse by multiplying it out. For example, in the last problem we found y3=3 for n3=5. Check: N3y3=42×3=126. Is 1261(mod5)? Yes, because 126 ends in a 6, so its remainder when divided by 5 is 1.
  • Forgetting the Final Modulo Step. The formula in Step 5 gives you a solution, but not necessarily the smallest positive one. Forgetting to calculate x0(modN) at the end is a very common mistake.
  • Simple Calculation Errors. There are many multiplications and additions involved. Take your time, write out each step, and use a calculator to check your arithmetic, especially for the large sum in Step 5.

Quick Summary and Reference

Here's a quick summary of the entire process for solving a system of congruences using the Chinese Remainder Theorem.

The Goal: Solve a system of congruences:

xai(modni)for i=1,2,,k

The Condition: All moduli, ni, must be pairwise coprime.

The Algorithm:

  1. Calculate the total product of the moduli: N=n1×n2××nk.
  2. For each congruence, calculate the partial product: Ni=N/ni.
  3. For each congruence, find the modular inverse yi that solves Niyi1(modni).
  4. Calculate the preliminary solution, x0, using the main formula:
    x0=i=1kaiNiyi=a1N1y1+a2N2y2++akNkyk
  5. Find the smallest positive solution, x, by taking the result modulo N: x=x0(modN).

Frequently Asked Questions

What is the Chinese Remainder Theorem actually used for in the real world?

It's crucial in modern cryptography, especially in the RSA algorithm, for speeding up complex calculations with large numbers. It's also used in computer science for algorithms that handle huge integers and in coding theory for creating error-correcting codes.

What happens if the moduli are not coprime?

The standard theorem doesn't apply directly, but a solution might still exist. You have to use a more advanced, generalized version of the theorem that first checks for contradictions between the congruences (for example, x1(mod4) and x2(mod2) is impossible).

Where does the name 'Chinese Remainder Theorem' come from?

The earliest known statement of this type of problem appeared in the 3rd-century book 'Sunzi Suanjing' (The Mathematical Classic of Sun Zi) by the Chinese mathematician Sun Tzu. The full algorithm for solving it was later generalized by other mathematicians.

Is the solution always a single number?

The solution is a whole family of numbers. The theorem gives you the unique smallest positive solution, but any number you get by adding or subtracting multiples of the big modulus N will also satisfy the system.

What does 'pairwise coprime' mean again?

It means if you take any two different moduli from your list, their greatest common divisor is 1. For example, in the set {6, 7, 25}, they are pairwise coprime because GCD(6,7)=1, GCD(6,25)=1, and GCD(7,25)=1.

Is there an easier way to find the modular inverse?

For the small numbers you see in class, trial and error (testing y=1, 2, 3...) is often the fastest way. For larger numbers, there is a more systematic method called the Extended Euclidean Algorithm, which is a topic you might learn in a more advanced number theory or discrete math class.

Can I solve these problems by just listing out multiples?

Yes, for very small numbers, that can be a good way to understand the problem and find the answer. However, the CRT provides a systematic method that works for any numbers, no matter how large, which is much more powerful and efficient than guessing or listing.