Chinese Remainder Theorem

When dividing numbers by (say) , we get remainders

this repeats all the time. In general, and have the same remainder (, their difference is divisible by ).

What if we consider the remainders for two different modulo at the same time?

First of all, we choose a pair like and for dividing a number for example .

  • The remainders modulo (i.e., and ) alternate (even values of alternate with odd values of ).

  • The remainders modulo (i.e., ) appear in a -loop ()

  • When we come to six, both cycles return to their original position (since is divisible both by and , and then the pairs start to repeat, thus forming a loop of length ).

  • All six possible combinations of remainders (two possible remainders modulo combined with three possible remainders modulo ) appear in this sequence (once per a -loop).

Let us look at the column for and : we see a cycle of length in this column formed by pairs - and, indeed, is divisible both by and , so we get again the pair . Note that not all combinations of remainders appear in the cycle: for example, pair does not appear.

Can you explain why this pair does not appear: why there are no m such that and ?

Again, the reason is simple: numbers of the form are even, while numbers of the form are odd.

To understand the behavior of remainders, it is useful to look at bigger examples, and draw the pairs of remainders on a coordinate plane. This can be done with the following code (do not worry if you do not understand the options, they are needed to make nice plots).

import matplotlib.pyplot as plt

a, b = 10, 5
n = a * b

plt.plot([i % a for i in range(n)], [i % b for i in range(n)],
         color='green', linestyle='dashed', linewidth=3,
         marker='o', markerfacecolor='blue', markersize=12)

plt.axis('square')
plt.xlim(-1, a)
plt.ylim(-1, b)
plt.xlabel(f'm mod {a}')
plt.ylabel(f'm mod {b}')

plt.savefig('crt-10-5.png')

In the first picture, the remainder modulo determines the remainder modulo . Indeed, if , then is divisible by , so .

A similar picture for and looks different:

Here we see that all pairs of remainders are possible. Why? This question is answered by the Chinese remainder theorem.

Theorem

Let be coprime. Then the system of equations

has a unique solution for modulo .

The reverse direction is trivial: given we can reduce modulo and modulo to obtain two equations of the above form.

The following is a general construction to find a solution to a system of congruences using the Chinese remainder theorem:

  1. Compute

  2. For each compute

  3. For each compute using Euclid's extended algorithm ( exists since are pairwise coprime).

  4. The integer is a solution to the system of congruences, and is the unique solution modulo .

When a system contains a relatively small number of congruences, an efficient process exists to apply the Chinese remainder theorem.

Solve the system of congruences

Take all the yi and construct the integer: We can prepare our situation to this: We wrote instead for inverse For find to general solve inverses of values For : We can use extended euclid algortihm find all values like this: and combine together:


Question-1 :  A box contains gold coins.

If the coins are equally divided among six friends, four coins are left over.

If the coins are equally divided among five friends, three coins are left over.

If the box holds the smallest number of coins that meets these two conditions, how many coins are left when equally divided among seven friends?

See answer ▼

Answer : 0

First of all, mathematize the question. So, We can prepare our situation For and : general equation: we found gold in our box. Now divide among seven friends Opps, zero coins left after divided our friends :)

Example

Find , if possible, such that

Solution First note that has an inverse modulo , namely . So we can write the first equivalence as . Hence, we can multiply every side of equation with inverse of . and we reached simple equation do it same process for other We can revise the question again and solve it like the previous ones. I left this to you :)




Question-1 :  What are the last two digits of ?

See answer ▼

Answer : 49

Observe that and . Then by the Chinese remainder theorem, the value is in correspondence with the solutions to the simultaneous congruences Now, Then the Chinese remainder theorem gives the value Therefore, the last two digits of are . Note that the above system of congruences is obtained for any odd exponent of , so the solution using the Chinese remainder theorem also gives that the last two digits of are for any positive odd value of .