The Chinese remainder theorem: matching two records
There is a pile of counters on the table, and we do not know its size. Grouping the counters in threes leaves two. Regrouping all the same counters in fives leaves three. Which totals could satisfy both records?
The Chinese remainder theorem studies this question: how can we combine the remainders of one integer after division by different numbers? The [[divisibility|divisibility]] page shows how division leaves a remainder; here we start again from the counters, so that page is not needed first. We will group the counters and keep the same example while we record remainders and find answers. This will show why some conditions can hold together while others conflict.
Full groups and what remains
Start with counters. Groups of fill groups and leave counters. The full groups contain counters; adding the remaining returns the original . The number left is called the remainder.
Why must the remainder be smaller than a full group? If counters were still left, we could fill another group, so the grouping would not be finished. The possible remainders after division by are therefore . A zero remainder means the groups use every counter.
Each solid box below is a full group; the dashed box holds the remainder. Change the total, then change the group size from to . Regrouping keeps the total fixed. Green counters belong to full groups; gold counters form the remainder.
Now let mean the integer we want, and the number of full groups. “Remainder after division by ” becomes . We count whole counters and whole groups, so and are integers.
Meet both records at once
The first record asks for remainder on division by . Start at and add a full group of each time. The remainder stays unchanged, giving . These are all the nonnegative possibilities for the first record: after removing the two leftover counters, only whole groups of three may remain.
For the second record, start at and add at a time, giving . We need a number appearing in both lists. Adding the two remainders is not the operation we need.
Each step below checks one candidate from the first list, then groups that same total in fives. Vertically aligned numbers in the two rows refer to the same total. A filled mark means the corresponding record is satisfied. Check , then , then .
Check against both records: and . It works. But is it the only answer? Next, we need a way to account for every possibility.
Columns name the same total in both rows. Filled circles mark a matching remainder; the highlighted column is the current candidate. On a narrow screen, scroll the table sideways.
| Total | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Divide by , leave | · | · | ● | · | · | ● | · | · | ● | · | · | ● | · | · | ● |
| Divide by , leave | · | · | · | ● | · | · | · | · | ● | · | · | · | · | ● | · |
| Candidate | Remainder after division by 5 | Both records? |
|---|---|---|
| 2 | 2 | No |
Why one interval is enough
Adding to a total adds five full groups of three, or three full groups of five, because . Either grouping gains only full groups, so neither remainder changes. Subtracting preserves the remainders too.
Every integer can be divided by to give , with . Here is an integer and is the remainder. For example, , while . Adding or subtracting multiples of therefore brings any integer into the interval from to .
So checking through cannot miss an entire family of answers. The first record leaves only in that interval. Their remainders on division by are respectively. Only leaves . Hence every integer answer is , where is any integer.
For the original pile of counters, a total cannot be negative, so we retain . For an integer problem, the full family extends in both directions: .
We can shorten the record with , read as “ is congruent to modulo ”. Here it says exactly that division by leaves , or that is an integer multiple of . The grouping number, , is called the modulus.
Prepare an integer tool
We have completely solved the example with and . Why do some other pairs of grouping numbers always give an answer, while others may fail? We need to examine the common factors of the two group sizes.
A positive integer dividing both numbers without a remainder is a common factor. The greatest one is their greatest common divisor, written . For example, the common factors of and are , so the greatest is . The only common factor of and is ; such a pair is called coprime.
How can we find the greatest common divisor and connect it to the original numbers? Divide with remainder: . A divisor of both and also divides . Conversely, a divisor of and also divides . Thus the pairs and have exactly the same common divisors.
Replace each pair by its divisor and remainder. Positive remainders strictly decrease, so eventually the remainder is zero. Every positive integer divides zero, so the last pair has greatest common divisor equal to its nonzero entry. That entry may be an earlier remainder, or the original divisor itself: for , the last pair is and the answer is . Each replacement preserved all common divisors, so this nonzero entry is also the original greatest common divisor. This procedure is the Euclidean algorithm.
Below, retain the original and while writing each new remainder as integer multiples of those two numbers added together. The first line is just : starting with the smaller number is permitted, and the following steps still work.
The original numbers 3 and 5 stay fixed. Each step replaces a pair by its divisor and remainder.
| Divide with remainder | The remainder in terms of the original numbers |
|---|
Follow the remainders back to their sources
The division giving remainder says . The preceding division says . Replace that by its expression, then distribute the minus sign and collect the two copies of :
This substitution always works: every remainder was the preceding dividend minus an integer multiple of the divisor. Replace these earlier remainders in turn until only the original two numbers remain. Addition, subtraction and multiplication of integers keep the coefficients integer. Thus, for any positive , their greatest common divisor can be written for some integers .
Derive the theorem
Now apply the tool to any coprime positive moduli . Their greatest common divisor is , so Euclidean division and substitution give integers with . These are the two coefficients of a combination like the one above; either may be negative.
We want an integer leaving remainder on division by , and remainder on division by , with and . First ensure the first record by putting , with integer still to be chosen. Whatever is, is a multiple of .
The second record asks for to be a multiple of . We therefore want the difference between and to be divisible by . Multiply the known equation by to make this difference:
Choose . The preceding equation then gives , a multiple of . Therefore meets both records. This proves existence for every allowed pair of remainders, rather than only for the examples we tried.
Next, how many different families of answers can there be? If both satisfy the records, their difference is divisible by both and . First write . We know that divides and need to show that it divides ; we cannot just erase without a reason.
Multiply by to get . The first term is a multiple of because is. The second is also a multiple of because it contains a factor of . Their sum is a multiple of , so for some integer . Thus .
Any two answers therefore differ by a multiple of . Conversely, adding any multiple of to an answer preserves both remainders. Divide a known answer by and keep its remainder : it is the unique answer with , and all answers are . This is the Chinese remainder theorem for two coprime moduli.
Return to the counters, one substitution at a time
When the moduli share a factor
What if the moduli are not coprime? Numbers leaving remainder on division by are odd; numbers leaving remainder on division by are even. An integer cannot be both, so these records conflict. Their common factor is where the conflict occurs.
Let be the greatest common divisor of the two moduli. A common answer would have . Rearranging gives . Since divides both , each term on the right is a multiple of . Therefore must be divisible by . This is a necessary condition.
The condition is also sufficient. Euclidean division gives . If , multiply by the integer to get . Choose . Its difference from is a multiple of , and its difference from is , a multiple of . Both records are satisfied.
Now find the exact repetition interval. Write and by removing the common factor. must be coprime: a common factor would make a common divisor of larger than , contradicting the choice of greatest common divisor.
The difference of two answers is a multiple of , say . It is also a multiple of , so is a multiple of . Since are coprime, the cancellation argument proved in the preceding chapter says that is a multiple of . The difference of answers is therefore a multiple of .
is itself a multiple of both and . The same argument shows that every common multiple must be a multiple of it. It is therefore their least common multiple, written . When answers exist, they repeat at precisely this interval.
Change the moduli and remainders below. Whether the difference of remainders is divisible by the greatest common divisor tells us whether an answer can exist. You can check this by inspecting the candidates. The table includes every candidate meeting the first record in one full repetition interval, so completing the table completes the check for these inputs.
Here names the repetition interval. In , the first record gives exactly candidates, separated by .
The difference is divisible by , so an answer exists. Every answer is , with integer . Inspect the candidates to locate its representative in this interval.
Add another record and practise
Combining two records gives another remainder record. For example, can be written . If we also require remainder on division by , combine that new record with the one we have just obtained.
Inspect . Their remainders on division by are , so we select . Because and are coprime, the new period is , giving . Check all original records: , , and .
With more records, repeat this combination. At each stage, check the greatest-common-divisor condition for the two current moduli. A conflict means no integer satisfies all records. Otherwise, the combined record retains all earlier conditions, ready for the next one.
If the original moduli are pairwise coprime, combining preserves the needed coprimality. To see why, suppose is coprime to both and . We have and . Multiplying gives , so any common divisor of and must divide . Repeating this argument gives the Chinese remainder theorem for more coprime moduli.
Find every integer leaving remainder modulo and remainder modulo .
One period is . Between and , the first condition gives . Their remainders modulo are , so only works. All answers are for integer . Check: .
Why do remainder modulo and remainder modulo work together?
The greatest common divisor is , which divides the remainder difference . The conditions are compatible. One period is . Candidates leave remainders modulo , selecting . All answers are ; indeed, .
Why can adding the product not repair remainder modulo and remainder modulo ?
The remainder difference is not divisible by the greatest common divisor . There is no starting answer. Adding preserves existing remainders, so it cannot change an incorrect remainder into the required one.
We have . What if a new record requires remainder modulo ?
Every number in the combined record leaves modulo : , and is a multiple of . The new remainder conflicts. The general condition agrees: , but is not a multiple of .
