Javascript required
Skip to content Skip to sidebar Skip to footer

Find All Solutions to the System of Congruences

Chinese Remainder Theorem

Given pairwise coprime positive integers n 1 , n 2 , , n k n_1, n_2, \ldots, n_k and arbitrary integers a 1 , a 2 , , a k a_1, a_2, \ldots, a_k , the system of simultaneous congruences

x a 1 ( m o d n 1 ) x a 2 ( m o d n 2 ) x a k ( m o d n k ) \begin{aligned} x &\equiv a_1 \pmod{n_1}\\ x &\equiv a_2 \pmod{n_2}\\ & \vdots\\ x &\equiv a_k \pmod{n_k} \end{aligned}

has a solution, and the solution is unique modulo N = n 1 n 2 n k N = n_1n_2\cdots n_k .

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

  1. Compute N = n 1 × n 2 × × n k N = n_1 \times n_2 \times \cdots \times n_k .

  2. For each i = 1 , 2 , , k i = 1, 2,\ldots, k , compute y i = N n i = n 1 n 2 n i 1 n i + 1 n k . y_i = \frac{N}{n_i} = n_1n_2 \cdots n_{i-1}n_{i+1} \cdots n_k.

  3. For each i = 1 , 2 , , k i = 1,2,\ldots, k , compute z i y i 1 m o d n i z_i \equiv y_i^{-1} \bmod{n_i} using Euclid's extended algorithm ( z i z_i exists since n 1 , n 2 , , n k n_1, n_2, \ldots, n_k are pairwise coprime).

  4. The integer x = i = 1 k a i y i z i x = \sum_{i=1}^{k} a_i y_i z_i is a solution to the system of congruences, and x m o d N x \bmod{N} is the unique solution modulo N N .

To see why x x is a solution, for each i = 1 , 2 , , k i = 1, 2, \ldots, k , we have

x ( a 1 y 1 z 1 + a 2 y 2 z 2 + + a k y k z k ) ( m o d n i ) a i y i z i ( m o d n i ) a i ( m o d n i ) , \begin{aligned} x & \equiv ( a_1 y_1 z_1 + a_2y_2z_2 + \cdots+ a_k y_k z_k) & \pmod{n_i}\\ & \equiv a_i y_i z_i & \pmod{n_i} \\ & \equiv a_i & \pmod{n_i}, \end{aligned}

where the second line follows since y j 0 m o d n i y_j \equiv 0 \bmod{n_i} for each j i j \neq i , and the third line follows since y i z i 1 m o d n i y_i z_i \equiv 1 \bmod{n_i} .

Now, suppose there are two solutions u u and v v to the system of congruences. Then n 1 ( u v ) , n 2 ( u v ) , , n k ( u v ) n_1 \lvert (u -v), n_2 \lvert (u-v), \ldots, n_k \lvert (u-v) , and since n 1 , n 2 , , n k n_1, n_2, \ldots, n_k are relatively prime, we have that n 1 n 2 n k n_1n_2\cdots n_k divides u v u-v , or

u v ( m o d n 1 n 2 n k ) . u \equiv v \pmod{n_1n_2\cdots n_k} .

Thus, the solution is unique modulo n 1 n 2 n k n_1n_2\cdots n_k . _\square

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

{ x 1 ( m o d 3 ) x 4 ( m o d 5 ) x 6 ( m o d 7 ) . \begin{cases}\begin{aligned} x &\equiv 1 \pmod{3} \\ x &\equiv 4 \pmod{5} \\ x &\equiv 6 \pmod{7}. \end{aligned}\end{cases}


Begin with the congruence with the largest modulus, x 6 ( m o d 7 ) . x \equiv 6 \pmod{7}. Rewrite this congruence as an equivalent equation:

x = 7 j + 6 ,  for some integer j . x=7j+6, \text{ for some integer }j.

Substitute this expression for x x into the congruence with the next largest modulus:

x 4 ( m o d 5 ) 7 j + 6 4 ( m o d 5 ) . x \equiv 4 \pmod{5} \implies 7j+6 \equiv 4 \pmod{5}.

Then solve this congruence for j : j:

j 4 ( m o d 5 ) . j \equiv 4 \pmod{5}.

Rewrite this congruence as an equivalent equation:

j = 5 k + 4 ,  for some integer k . j=5k+4, \text{ for some integer }k.

Substitute this expression for j j into the expression for x : x:

x = 7 ( 5 k + 4 ) + 6 x = 35 k + 34. \begin{aligned} x &= 7(5k+4)+6 \\ x &= 35k+34. \\ \end{aligned}

Now substitute this expression for x x into the final congruence, and solve the congruence for k : k:

35 k + 34 1 ( m o d 3 ) k 0 ( m o d 3 ) . \begin{aligned} 35k+34 &\equiv 1 \pmod{3} \\ k &\equiv 0 \pmod{3}. \end{aligned}

Write this congruence as an equation, and then substitute the expression for k k into the expression for x : x:

k = 3 l ,  for some integer l . x = 35 ( 3 l ) + 34 x = 105 l + 34. \begin{aligned} k &= 3l, \text{ for some integer }l. \\ x &= 35(3l)+34 \\ x &= 105l+34. \end{aligned}

This equation implies the congruence

x 34 ( m o d 105 ) . x \equiv 34 \pmod{105}.

This happens to be the solution to the system of congruences. \ _\square

Process to solve systems of congruences with the Chinese remainder theorem:

For a system of congruences with co-prime moduli, the process is as follows:

  • Begin with the congruence with the largest modulus, x a k ( m o d n k ) . x \equiv a_k \pmod{n_k}. Re-write this modulus as an equation, x = n k j k + a k , x=n_kj_k+a_k, for some positive integer j k . j_k.

  • Substitute the expression for x x into the congruence with the next largest modulus, x a k ( m o d n k ) n k j k + a k a k 1 ( m o d n k 1 ) . x \equiv a_k \pmod{n_k} \implies n_kj_k+a_k \equiv a_{k-1} \pmod{n_{k-1}}.

  • Solve this congruence for j k . j_k.

  • Write the solved congruence as an equation, and then substitute this expression for j k j_k into the equation for x . x.

  • Continue substituting and solving congruences until the equation for x x implies the solution to the system of congruences.

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?

The Chinese remainder theorem can be applied to systems with moduli that are not co-prime, but a solution to such a system does not always exist.

Solve the system of congruences

{ x 5 ( m o d 6 ) x 3 ( m o d 8 ) . \begin{cases}\begin{aligned} x &\equiv 5 \pmod{6} \\ x &\equiv 3 \pmod{8}. \\ \end{aligned}\end{cases}


Note that the greatest common divisor of the moduli is 2. The first congruence implies x 1 ( m o d 2 ) x \equiv 1\pmod {2} and the second congruence also implies x 1 ( m o d 2 ) . x \equiv 1 \pmod{2}. Therefore, there is no conflict between these two congruences. In fact, the system of congruences can be reduced to a simpler system of congruences by dividing out the GCD of the moduli from the modulus of the first congruence:

{ x 2 ( m o d 3 ) x 3 ( m o d 8 ) . \begin{cases}\begin{aligned} x &\equiv 2 \pmod{3} \\ x &\equiv 3 \pmod{8}. \\ \end{aligned}\end{cases}

Write the second congruence as an equation:

x = 8 j + 3. x=8j+3.

Substitute into the first congruence and solve for j : j:

8 j + 3 2 ( m o d 3 ) j 1 ( m o d 3 ) . \begin{aligned} 8j+3 &\equiv 2 \pmod{3} \\ j &\equiv 1 \pmod{3}. \end{aligned}

Write this congruence as an equation, and then substitute into the equation for x : x:

j = 3 k + 1 x = 8 ( 3 k + 1 ) + 3 x = 24 k + 11. \begin{aligned} j &= 3k+1 \\ x &= 8(3k+1)+3 \\ x &= 24k+11. \end{aligned}

This gives x 11 ( m o d 24 ) x \equiv 11 \pmod{24} as the solution to the system of congruences. Note that lcm ( 6 , 8 ) = 24. \text{lcm}(6,8)=24. _\square

The number of students in a school is between 500 and 600. If we group them into groups of 12, 20, or 36 each, 7 students are always left over. How many students are in this school?

Whether or not a system of congruences has solutions depends on if there are any conflicts between pairs of congruences.

Show that there are no solutions to the system of congruences:

{ x 2 ( m o d 6 ) x 5 ( m o d 9 ) x 7 ( m o d 15 ) . \begin{cases}\begin{aligned} x &\equiv 2 \pmod{6} \\ x &\equiv 5 \pmod{9} \\ x &\equiv 7 \pmod{15}. \end{aligned}\end{cases}


Note that each modulus is divisible by 3. The first and second congruences imply that x 2 ( m o d 3 ) . x \equiv 2 \pmod{3}. However, the third congruence implies that x 1 ( m o d 3 ) . x \equiv 1 \pmod{3}. Since these both cannot be true, there are no solutions to the system of congruences. _\square

A system of linear congruences has solutions if and only if for every pair of congruences within the system,

{ x a i ( m o d n i ) x a j ( m o d n j ) , a i a j ( mod gcd ( n i , n j ) ) . \begin{aligned} \begin{cases} x \equiv a_i \pmod{n_i} \\ x \equiv a_j \pmod{n_j},\qquad \end{cases} a_i &\equiv a_j\ \ \big(\text{mod }\ {\gcd(n_i,n_j)}\big). \end{aligned}

Furthermore, if solutions exist, then they are of the form

x b ( mod lcm ( n 1 , n 2 , , n k ) ) x \equiv b\ \ \big(\text{mod }\ {\text{lcm}(n_1,n_2, \ldots , n_k)}\big)

for some integer b . b.

Brahmagupta has a basket full of eggs. When he takes the eggs out of the basket 2 at a time, there is 1 egg left over. When he takes them out 3 at a time, there are 2 eggs left over. Likewise, when he takes the eggs out 4, 5, and 6 at a time, he finds remainders of 3, 4, and 5, respectively. However, when he takes the eggs out 7 at a time, there are no eggs left over.

What is the least amount of eggs that could be in Brahmagupta's basket?

The real life application of the Chinese remainder theorem might be of interest to the reader, so we will give one such example here. One use is in astronomy where k k events may occur regularly, with periods n 1 , n 2 , , n k n_{1}, n_{2}, \ldots, n_{k} and with the i th i^\text{th} event happening at times x = a i , a i + n i , a i + 2 n i , . x = a_i, a_i+n_i, a_i+2n_i, \ldots. This means that the k k events occur simultaneously at time x , x, where x = a i m o d n i x = a_i \bmod{n_i} for all i {i} . A simple illustration of this is the orbit of planets and moons, as well as eclipses.

Comets 2P/Encke, 4P/Faye, and 8P/Tuttle have orbital periods of 3 years, 8 years, and 13 years, respectively. The last perihelions of each of these comets were in 2017, 2014, and 2008, respectively.

What is the next year in which all three of these comets will achieve perihelion in the same year?

For this problem, assume that time is measured in whole numbers of years and that each orbital period is constant.

Sometimes, a problem will lend itself to using the Chinese remainder theorem "in reverse." That is, when a problem requires you to compute a remainder with a composite modulus, it can be worthwhile to consider that modulus's prime power divisors. Then, the Chinese remainder theorem will guarantee a unique solution in the original modulus.

What are the last two digits of 4 9 19 ? 49^{19}?


Observe that 100 = 25 × 4 100 = 25 \times 4 and gcd ( 25 , 4 ) = 1 \gcd(25,4) = 1 . Then by the Chinese remainder theorem, the value x 4 9 19 m o d 100 x \equiv 49^{19} \bmod{100} is in correspondence with the solutions to the simultaneous congruences

x 4 9 19 ( m o d 25 ) x 4 9 19 ( m o d 4 ) . \begin{aligned} x \equiv 49^{19} &\pmod{25}\\ x \equiv 49^{19} &\pmod{4}. \end{aligned}

Now,

4 9 19 ( 1 ) 19 1 ( m o d 25 ) 4 9 19 ( 1 ) 19 1 ( m o d 4 ) . \begin{aligned} 49^{19} \equiv (-1)^{19} &\equiv -1 &\pmod{25}\\ 49^{19} \equiv (1)^{19} &\equiv 1 &\pmod{4}. \end{aligned}

Then the Chinese remainder theorem gives the value

x ( ( 1 ) ( 4 ) ( 19 ) + ( 1 ) ( 25 ) ( 1 ) ) ( m o d 100 ) ( 76 + 25 ) ( m o d 100 ) 51 ( m o d 100 ) 49 ( m o d 100 ) . \begin{aligned} x &\equiv \big((-1)(4)(19) + (1)(25)(1)\big) &\pmod{100}\\ & \equiv ( -76 + 25) &\pmod{100}\\ & \equiv -51 &\pmod{100}\\ &\equiv 49 &\pmod{100}. \end{aligned}

Therefore, the last two digits of 4 9 19 49^{19} are 49. Note that the above system of congruences is obtained for any odd exponent of 49, so the solution using the Chinese remainder theorem also gives that the last two digits of 4 9 k 49^k are 49 for any positive odd value of k k . _\square

The Chinese remainder theorem can be useful for proofs.

Show that there exist 99 99 consecutive integers a 1 , a 2 , , a 99 a_1, a_2, \ldots, a_{99} such that each a i a_i is divisible by the cube of some integer greater than 1.


Let p 1 , p 2 , , p 99 p_1, p_2, \ldots, p_{99} be distinct prime numbers. Now, consider the simultaneous congruences

x 1 ( m o d p 1 3 ) x 2 ( m o d p 2 3 ) x 99 ( m o d p 99 3 ) . \begin{aligned} x & \equiv -1 \pmod{p_1^3}\\ x & \equiv -2 \pmod{p_2^3}\\ & \vdots\\ x & \equiv -99 \pmod{p_{99}^3}.\\ \end{aligned}

Since p i p_i are pairwise coprime, this system of equations has a solution by the Chinese remainder theorem. Then the integers a i = x + i a_i = x+i for i = 1 , 2 , , 99 i = 1,2, \ldots, 99 are 99 consecutive integers such that p i 3 p_i^3 divides a i a_i . _\square

Try these problems to test what you know.

Find the smallest whole number that, when divided by 5, 7, 9, and 11, gives remainders of 1, 2, 3, and 4, respectively.

A general counts the number of surviving soldiers of a battle by aligning them successively in rows of certain sizes. Each time, he counts the number of remaining soldiers who failed to fill a row. The general initially had 1200 soldiers before the battle; after the battle

  • aligning them in rows of 5 soldiers leaves 3 remaining soldiers;
  • aligning them in rows of 6 soldiers leaves 3 remaining soldiers;
  • aligning them in rows of 7 soldiers leaves 1 remaining soldier;
  • aligning them in rows of 11 soldiers leaves 0 remaining soldiers.

How many soldiers survived the battle?

Four friends--let's call them A, B, C, and D--are planning to go to the concert, but they realize that they are a few dollars short to buy the tickets ($50 per ticket).

We know that each of them has an integer amount of dollars and that

if B B borrowed $ 1 1 from A A , then B B would have 2 3 \frac{2}{3} of A A 's balance;
if C C borrowed $ 2 2 from B B , then C C would have 3 5 \frac{3}{5} of B B 's balance;
if D D borrowed $ 3 3 from C C , then D D would have 5 7 \frac{5}{7} of C C 's balance.

At least how much more money (in $) do they need all together in order to afford 4 tickets?

Find the last three digits of the number

3 × 7 × 11 × 15 × × 2003. 3 \times 7 \times 11 \times 15 \times \cdots \times 2003.

You are building a rainbow building from N N cubic unit blocks.

First, you put together 3 cubes each to form a group of 1 × 3 1\times 3 columns and discard the remaining cubes.

Then, you put together 5 columns each to form a group of 3 × 5 3\times 5 cubic bases and discard the remaining columns, as before.

Finally, you stack 7 bases over one another to build the desired 3 × 5 × 7 3\times 5\times 7 cuboid structure, as shown above, and discard all the other bases, as usual.

If there are a total of 52 discarded cubes, and N N is a multiple of 11, what is the least possible value of N ? N?

What is the remainder when 12 34 56 78 \Huge \color{#D61F06}{12}^{\color{#20A900}{34}^{\color{#3D99F6}{56}^{\color{#624F41}{78}}}} is divided by 90 ? \color{#302B94}{90}?

2017 ! \large 2017!

Find the last two non-zero digits in the number above.

  • Fermat's Little Theorem

  • Linear Diophantine Equations

  • Wilson's Theorem

Find All Solutions to the System of Congruences

Source: https://brilliant.org/wiki/chinese-remainder-theorem/