Difference between revisions of "Chinese Remainder Theorem"

(Theorem)
(Formatting)
 
(13 intermediate revisions by 11 users not shown)
Line 1: Line 1:
The '''Chinese Remainder Theorem''' is a [[number theory | number theoretic]] result. It is one of the only [[theorem]]s named for an oriental person or place, due to the closed development of mathematics in the western world.
+
The '''Chinese Remainder Theorem''' is a [[number theory|number theoretic]] result.
== Theorem ==
 
  
Formally stated, the Chinese Remainder Theorem is as follows:
+
== Statement ==
  
Let <math>m</math> be [[relatively prime]] to <math>n</math>. Then each [[residue class]] mod <math>mn</math> is equal to the [[intersection]] of a unique residue class mod <math>m</math> and a unique residue class mod <math>n</math>, and the intersection of each residue class mod <math>m</math> with a residue class mod <math>n</math> is a residue class mod <math>mn</math>.
+
Let <math>m</math> be [[relatively prime]] to <math>n</math>. Then each [[residue class]] mod <math>mn</math> is equal to the [[intersection]] of a unique residue class mod <math>m</math> and a unique residue class mod <math>n</math>, and the intersection of each residue class mod <math>m</math> with a residue class mod <math>n</math> is a residue class mod <math>mn</math>.
  
 +
This means that if we have <math>b \equiv c \pmod {mn}</math> we can deduce that <math>b \equiv c \pmod{m}</math> and <math>b \equiv c \pmod{n}.</math>
  
Simply stated:
 
  
Suppose you wish to find the least number <math>x</math> which leaves a remainder of:
+
In other words, suppose you wish to find the least number <math>x</math> which leaves a remainder of
  
<center>
+
<center><math>\begin{aligned}
<math> \begin{aligned} &y_{1} \text{ when divided by } &d_{1}\\
+
&y_{1} &\text{when divided by} &&d_{1}\\
&y_{2} \text{ when divided by } &d_{2}\\
+
&y_{2} &\text{when divided by} &&d_{2}\\
&\vdots &\vdots\\
+
&\vdots &&&\vdots\\
&y_{n} \text{ when divided by } & d_{n}\\ \end{aligned} </math>
+
&y_{n} &\text{when divided by} &&d_{n}\\
</center>
+
\end{aligned}</math></center>
  
such that <math>d_{1}</math> , <math>d_{2}</math> , ... <math>d_{n}</math> are all relatively prime.  
+
such that <math>d_{1},d_{2}, \ldots ,d_{n}</math> are all relatively prime.  
Let <math>M = d_{1}d_{2} \cdots d_{n}</math>, and <math>b_{i} = \frac{M}{d_{i}}</math>.  
+
Let <math>M = d_{1}d_{2} \cdots d_{n}</math>, and <math>b_{i} = \frac{M}{d_{i}}</math>. If the numbers <math>a_{i}</math> satisfy <math>a_{i}b_{i} \equiv 1 \pmod {d_{i}}</math>, for every <math>1 \leq i \leq n</math>, then a solution for <math>x</math> is <math>\sum_{i=1}^n a_{i}b_{i}y_{i} \pmod M</math>
Now if the numbers <math>a_{i}</math> satisfy:
 
<center>
 
<math>a_{i}b_{i} - 1 \equiv 0 \pmod {d_{i}} </math>
 
</center>
 
for every <math>1 \leq i \leq n</math>, then a solution for <math>x</math> is:
 
<center>
 
<math>x = \sum_{i=1}^n a_{i}b_{i}y_{i} \pmod M</math>
 
</center>
 
  
 
== Proof ==
 
== Proof ==
  
If <math>a \equiv b \pmod{mn}</math>, then <math>a</math> and <math>b</math> differ by a multiple of <math>mn</math>, so <math>a \equiv b \pmod{m}</math> and <math>a \equiv b \pmod{n}</math>. This is the first part of the theorem. The converse follows because <math>a</math> and <math>b</math> must differ by a multiple of <math>m</math> and <math>n</math>, and <math>\mbox{lcm}(m,n) = mn</math>. This is the second part of the theorem.
+
If <math>a \equiv b \pmod{mn}</math>, then <math>a</math> and <math>b</math> differ by a multiple of <math>mn</math>, so <math>a \equiv b \pmod{m}</math> and <math>a \equiv b \pmod{n}</math>. This is the first part of the theorem. The converse follows because <math>a</math> and <math>b</math> must differ by a multiple of <math>m</math> and <math>n</math>, and <math>\mbox{lcm}(m,n) = mn</math>. This is the second part of the theorem.
  
 
== Applicability ==
 
== Applicability ==
  
Much like the [[Fundamental Theorem of Arithmetic]], many people seem to take this theorem for granted before they consciously turn their attention to it. It ubiquity derives from the fact that many results can be easily proven mod (a power of a prime), and can then be generalized to mod <math>m</math> using the Chinese Remainder Theorem. For instance, [[Fermat's Little Theorem]] may be generalized to the [[Fermat-Euler Theorem]] in this manner.
+
Much like the [[Fundamental Theorem of Arithmetic]], many people seem to take this theorem for granted before they consciously turn their attention to it. Its ubiquity derives from the fact that many results can be easily proven mod (a power of a prime), and can then be generalized to mod <math>m</math> using the Chinese Remainder Theorem. For instance, [[Fermat's Little Theorem]] may be generalized to the [[Fermat-Euler Theorem]] in this manner.
  
 
'''General Case''': the proof of the general case follows by induction to the above result (k-1) times.
 
'''General Case''': the proof of the general case follows by induction to the above result (k-1) times.
  
==Extended version of the theorem==
+
=== Solving a system of congruences using CRT ===
Suppose one tried to divide a group of fish into <math>2</math>, <math>3</math> and <math>4</math> parts instead and found <math>1</math>, <math>1</math> and <math>2</math> fish left over, respectively. Any number with remainder <math>1</math> mod <math>2</math> must be [[odd integer | odd]] and any number with remainder <math>2</math> mod <math>4</math> must be [[even integer | even]]. Thus, the number of objects must be odd and even simultaneously, which is a contradiction. Thus, there must be restrictions on the numbers <math>a_1,\dots,a_n</math> to ensure that at least one solution exists. It follows that:
+
 
 +
In order to solve a system of n congruences, it is typical to solve the first two, then combine that with the third, and so on. So, it suffices to know how solve a system of 2 congruences.
 +
 
 +
Let the system be (where <math>m</math> and <math>n</math> are relatively coprime):
 +
 
 +
<cmath>x\equiv a \mod m</cmath>
 +
<cmath>x\equiv b \mod n</cmath>
 +
 
 +
Then if we find one value <math>k</math> such that <math>x=k</math> satisfies the system, then the solution set consists of <math>x\equiv k \mod mn</math>. To find such <math>k</math>, set <math>x=cm+a=dn+b</math>. Then, find <math>c, d</math> that satisfy the equality. This is usually easier than brute forcing for <math>k</math>.
 +
 
 +
Let's take an example:
 +
<cmath>x\equiv 1 \mod 2</cmath>
 +
<cmath>4x\equiv 3 \mod 5</cmath>
 +
First simplify the second equation to <math>x\equiv 3\cdot 4 \equiv 2 \mod 5</math> using modular inverses. So we have:
 +
<cmath>x\equiv 1 \mod 2</cmath>
 +
<cmath>x\equiv 2 \mod 5</cmath>
 +
Then let <math>x=2a+1=5b+2</math>. A clear solution <math>a,b</math> for this is <math>a=3, b=1</math>. Then, <math>x=7</math> is one solution to the system, so <math>x\equiv 7 \mod 10</math> is the set of all solutions.
 +
 
 +
If <math>m</math> and <math>n</math> are not relatively prime, then let <math>\gcd(m, n)=g</math>. We split the system as follows:
 +
<cmath>x\equiv a \mod \frac{m}{g}</cmath>
 +
<cmath>x\equiv a \mod g</cmath>
 +
<cmath>x\equiv b \mod g</cmath>
 +
<cmath>x\equiv b \mod \frac{n}{g}</cmath>
 +
Then, we must check that <math>a\equiv b\mod g</math>. If so, simply ignore the 3rd congruence. Now, we have:
 +
<cmath>x\equiv a \mod \frac{m}{g}</cmath>
 +
<cmath>x\equiv a \mod g</cmath>
 +
<cmath>x\equiv b \mod \frac{n}{g}</cmath>
 +
Now we have a system of 3 congruences, which we can solve for. If <math>\gcd(\frac{m}{g}, g)</math> is not <math>1</math>, then repeat the decomposition. Essentially, decompose until we get a system of pairwise relatively prime congruences. Then solve.
 +
 
 +
== Extended version of the theorem ==
 +
Suppose one tried to divide a group of objects into <math>2</math>, <math>3</math> and <math>4</math> parts instead and found <math>1</math>, <math>1</math> and <math>2</math> objects left over, respectively. Any number with remainder <math>1</math> mod <math>2</math> must be [[odd integer|odd]] and any number with remainder <math>2</math> mod <math>4</math> must be [[even integer|even]]. Thus, the number of objects must be odd and even simultaneously, which is a contradiction. Thus, there must be restrictions on the numbers <math>a_1,\dots,a_n</math> to ensure that at least one solution exists. It follows that:
  
 
''The solution exists if and only if <math>a_i\equiv a_j\mod \gcd(m_i,m_j)</math> for all <math>i,j</math> where <math>\gcd</math> stands for the [[greatest common divisor]]. Moreover, in the case when the problem is solvable, any two solutions differ by some [[common multiple]] of <math>m_1,\ldots,m_n</math>.'' (the extended version).
 
''The solution exists if and only if <math>a_i\equiv a_j\mod \gcd(m_i,m_j)</math> for all <math>i,j</math> where <math>\gcd</math> stands for the [[greatest common divisor]]. Moreover, in the case when the problem is solvable, any two solutions differ by some [[common multiple]] of <math>m_1,\ldots,m_n</math>.'' (the extended version).
Line 48: Line 69:
 
*[[Chicken McNugget Theorem]]
 
*[[Chicken McNugget Theorem]]
  
==Discussion==
+
=== External links ===
* [http://www.artofproblemsolving.com/Forum/viewtopic.php?t=80124&sid=df9439496046e0ff9f97cfc644408395 Here] is an AoPS thread in which the Chinese Remainder Theorem is discussed and implemented.
+
* [{{SERVER}}/community/c324h80124 Here] is an AoPS thread in which the Chinese Remainder Theorem is discussed and implemented.
  
 
[[Category:Number theory]]
 
[[Category:Number theory]]
 
[[Category:Theorems]]
 
[[Category:Theorems]]

Latest revision as of 18:33, 11 March 2025

The Chinese Remainder Theorem is a number theoretic result.

Statement

Let $m$ be relatively prime to $n$. Then each residue class mod $mn$ is equal to the intersection of a unique residue class mod $m$ and a unique residue class mod $n$, and the intersection of each residue class mod $m$ with a residue class mod $n$ is a residue class mod $mn$.

This means that if we have $b \equiv c \pmod {mn}$ we can deduce that $b \equiv c \pmod{m}$ and $b \equiv c \pmod{n}.$


In other words, suppose you wish to find the least number $x$ which leaves a remainder of

$\begin{aligned} &y_{1} &\text{when divided by} &&d_{1}\\ &y_{2} &\text{when divided by} &&d_{2}\\ &\vdots &&&\vdots\\ &y_{n} &\text{when divided by} &&d_{n}\\ \end{aligned}$

such that $d_{1},d_{2}, \ldots ,d_{n}$ are all relatively prime. Let $M = d_{1}d_{2} \cdots d_{n}$, and $b_{i} = \frac{M}{d_{i}}$. If the numbers $a_{i}$ satisfy $a_{i}b_{i} \equiv 1 \pmod {d_{i}}$, for every $1 \leq i \leq n$, then a solution for $x$ is $\sum_{i=1}^n a_{i}b_{i}y_{i} \pmod M$

Proof

If $a \equiv b \pmod{mn}$, then $a$ and $b$ differ by a multiple of $mn$, so $a \equiv b \pmod{m}$ and $a \equiv b \pmod{n}$. This is the first part of the theorem. The converse follows because $a$ and $b$ must differ by a multiple of $m$ and $n$, and $\mbox{lcm}(m,n) = mn$. This is the second part of the theorem.

Applicability

Much like the Fundamental Theorem of Arithmetic, many people seem to take this theorem for granted before they consciously turn their attention to it. Its ubiquity derives from the fact that many results can be easily proven mod (a power of a prime), and can then be generalized to mod $m$ using the Chinese Remainder Theorem. For instance, Fermat's Little Theorem may be generalized to the Fermat-Euler Theorem in this manner.

General Case: the proof of the general case follows by induction to the above result (k-1) times.

Solving a system of congruences using CRT

In order to solve a system of n congruences, it is typical to solve the first two, then combine that with the third, and so on. So, it suffices to know how solve a system of 2 congruences.

Let the system be (where $m$ and $n$ are relatively coprime):

\[x\equiv a \mod m\] \[x\equiv b \mod n\]

Then if we find one value $k$ such that $x=k$ satisfies the system, then the solution set consists of $x\equiv k \mod mn$. To find such $k$, set $x=cm+a=dn+b$. Then, find $c, d$ that satisfy the equality. This is usually easier than brute forcing for $k$.

Let's take an example: \[x\equiv 1 \mod 2\] \[4x\equiv 3 \mod 5\] First simplify the second equation to $x\equiv 3\cdot 4 \equiv 2 \mod 5$ using modular inverses. So we have: \[x\equiv 1 \mod 2\] \[x\equiv 2 \mod 5\] Then let $x=2a+1=5b+2$. A clear solution $a,b$ for this is $a=3, b=1$. Then, $x=7$ is one solution to the system, so $x\equiv 7 \mod 10$ is the set of all solutions.

If $m$ and $n$ are not relatively prime, then let $\gcd(m, n)=g$. We split the system as follows: \[x\equiv a \mod \frac{m}{g}\] \[x\equiv a \mod g\] \[x\equiv b \mod g\] \[x\equiv b \mod \frac{n}{g}\] Then, we must check that $a\equiv b\mod g$. If so, simply ignore the 3rd congruence. Now, we have: \[x\equiv a \mod \frac{m}{g}\] \[x\equiv a \mod g\] \[x\equiv b \mod \frac{n}{g}\] Now we have a system of 3 congruences, which we can solve for. If $\gcd(\frac{m}{g}, g)$ is not $1$, then repeat the decomposition. Essentially, decompose until we get a system of pairwise relatively prime congruences. Then solve.

Extended version of the theorem

Suppose one tried to divide a group of objects into $2$, $3$ and $4$ parts instead and found $1$, $1$ and $2$ objects left over, respectively. Any number with remainder $1$ mod $2$ must be odd and any number with remainder $2$ mod $4$ must be even. Thus, the number of objects must be odd and even simultaneously, which is a contradiction. Thus, there must be restrictions on the numbers $a_1,\dots,a_n$ to ensure that at least one solution exists. It follows that:

The solution exists if and only if $a_i\equiv a_j\mod \gcd(m_i,m_j)$ for all $i,j$ where $\gcd$ stands for the greatest common divisor. Moreover, in the case when the problem is solvable, any two solutions differ by some common multiple of $m_1,\ldots,m_n$. (the extended version).

See Also

External links

  • Here is an AoPS thread in which the Chinese Remainder Theorem is discussed and implemented.