maths.freeNumber Theory › Congruences › Chinese remainder theorem

Chinese remainder theorem

In mathematics, the Chinese remainder theorem states that if one knows the remainders of the Euclidean division of an integer n by several integers, then one can determine uniquely the remainder of the division of n by…

Chinese remainder theorem

In mathematics, the Chinese remainder theorem states that if one knows the remainders of the Euclidean division of an integer n by several integers, then one can determine uniquely the remainder of the division of n by the product of these integers, under the condition that the divisors are pairwise coprime (no two divisors share a common factor other than 1).

The theorem is sometimes called Sunzi's theorem. Both names of the theorem refer to its earliest known statement that appeared in Sunzi Suanjing, a Chinese manuscript written during the 3rd to 5th century CE. This first statement was restricted to the following example:

If one knows that the remainder of n divided by 3 is 2, the remainder of n divided by 5 is 3, and the remainder of n divided by 7 is 2, then with no other information, one can determine the remainder of n divided by 105 (the product of 3, 5, and 7) without knowing the value of n. In this example, the remainder is 23. Moreover, this remainder is the only possible positive value of n that is less than 105.

The Chinese remainder theorem is widely used for computing with large integers, as it allows replacing a computation for which one knows a bound on the size of the result by several similar computations on small integers.

The Chinese remainder theorem (expressed in terms of congruences) is true over every principal ideal domain. It has been generalized to any ring, with a formulation involving two-sided ideals.

History

The earliest known statement of the problem appears in the 5th-century book Sunzi Suanjing by the Chinese mathematician Sunzi:

Sunzi's work would not be considered a theorem by modern standards; it only gives one particular problem, without showing how to solve it, much less any proof about the general case or a general algorithm for solving it. An algorithm for solving this problem was described by Aryabhata (6th century). Special cases of the Chinese remainder theorem were also known to Brahmagupta (7th century) and appear in Fibonacci's Liber Abaci (1202). The result was later generalized with a complete solution called Da-yan-shu (大衍術) in Qin Jiushao's 1247 Mathematical Treatise in Nine Sections which was translated into English in early 19th century by British missionary Alexander Wylie.

The notion of congruences was first introduced and used by Carl Friedrich Gauss in his Disquisitiones Arithmeticae of 1801. Gauss illustrates the Chinese remainder theorem on a problem involving calendars, namely, "to find the years that have a certain period number with respect to the solar and lunar cycle and the Roman indiction." Gauss introduces a procedure for solving the problem that had already been used by Leonhard Euler but was in fact an ancient method that had appeared several times.

Statement

Let n1, ..., nk be integers greater than 1, which are often called moduli or divisors. Let us denote by N the product of the ni.

The Chinese remainder theorem asserts that if the ni are pairwise coprime, and if a1, ..., ak are integers such that 0 ≤ ai < ni for every i, then there is one and only one integer x, such that 0 ≤ x < N and the remainder of the Euclidean division of x by ni is ai for every i.

This may be restated as follows in terms of congruences: If the \(n_i\) are pairwise coprime, and if a1, ..., ak are any integers, then the system

\(\begin{align} x &\equiv a_1 \pmod{n_1} \\ &\,\,\,\vdots \\ x &\equiv a_k \pmod{n_k}, \end{align}\)

has a solution, and any two solutions, say x1 and x2, are congruent modulo N, that is, x1x2 (mod N ).

In abstract algebra, the theorem is often restated as: if the ni are pairwise coprime, the map

\(x \bmod N \;\mapsto\;(x \bmod n_1,\, \ldots,\, x \bmod n_k)\)

defines a ring isomorphism

\(\mathbb{Z}/N\mathbb{Z} \cong \mathbb{Z}/n_1\mathbb{Z} \times \cdots \times \mathbb{Z}/n_k\mathbb{Z}\)

between the ring of integers modulo N and the direct product of the rings of integers modulo the ni. This means that for doing a sequence of arithmetic operations in \(\mathbb{Z}/N\mathbb{Z},\) one may do the same computation independently in each \(\mathbb{Z}/n_i\mathbb{Z}\) and then get the result by applying the isomorphism (from the right to the left). This may be much faster than the direct computation if N and the number of operations are large. This is widely used, under the name multi-modular computation, for linear algebra over the integers or the rational numbers.

Condensed: the full section is in Wikipedia.

Proof

The existence and the uniqueness of the solution may be proven independently. However, the first proof of existence, given below, uses this uniqueness.

Uniqueness

Suppose that x and y are both solutions to all the congruences. As x and y give the same remainder, when divided by ni, their difference xy is a multiple of each ni. As the ni are pairwise coprime, their product N also divides xy, and thus x and y are congruent modulo N. If x and y are supposed to be non-negative and less than N (as in the first statement of the theorem), then their difference may be a multiple of N only if x = y.

Existence (first proof)

The map

\(x \bmod N \mapsto (x \bmod n_1, \ldots, x\bmod n_k)\)

maps congruence classes modulo N to sequences of congruence classes modulo ni. The proof of uniqueness shows that this map is injective. As the domain and the codomain of this map have the same number of elements, the map is also surjective, which proves the existence of the solution.

This proof is very simple but does not provide any direct way for computing a solution. Moreover, it cannot be generalized to other situations where the following proof can.

Existence (constructive proof)

Existence may be established by an explicit construction of x. This construction may be split into two steps, first solving the problem in the case of two moduli, and then extending this solution to the general case by induction on the number of moduli.

Existence (direct construction)

For constructing a solution, it is not necessary to make an induction on the number of moduli. However, such a direct construction involves more computation with large numbers, which makes it less efficient and less used. Nevertheless, Lagrange interpolation is a special case of this construction, applied to polynomials instead of integers.

Let \(N_i = N/n_i\) be the product of all moduli but one. As the \(n_i\) are pairwise coprime, \(N_i\) and \(n_i\) are coprime. Thus Bézout's identity applies, and there exist integers \(M_i\) and \(m_i\) such that

\(M_iN_i + m_in_i=1.\)

A solution of the system of congruences is

\(x=\sum_{i=1}^k a_iM_iN_i.\)

In fact, as \(N_j\) is a multiple of \(n_i\) for \(i\neq j,\) we have

\(x \equiv a_iM_iN_i \equiv a_i(1-m_in_i) \equiv a_i \pmod{n_i},\)

for every \(i.\)

Computation

Consider a system of congruences:

\(\begin{align} x &\equiv a_1 \pmod{n_1} \\ &\vdots \\ x &\equiv a_k \pmod{n_k}, \\ \end{align}\)

where the \(n_i\) are pairwise coprime, and let \(N=n_1 n_2\cdots n_k.\) In this section several methods are described for computing the unique solution for \(x\), such that \(0\le x

\(\begin{align} x &\equiv 0 \pmod 3 \\ x &\equiv 3 \pmod 4 \\ x &\equiv 4 \pmod 5. \end{align}\)

Several methods of computation are presented. The two first ones are useful for small examples, but become very inefficient when the product \(n_1\cdots n_k\) is large. The third one uses the existence proof given in § Existence (constructive proof). It is the most convenient when the product \(n_1\cdots n_k\) is large, or for computer computation.

Systematic search

It is easy to check whether a value of x is a solution: it suffices to compute the remainder of the Euclidean division of x by each ni. Thus, to find the solution, it suffices to check successively the integers from 0 to N until finding the solution.

Although very simple, this method is very inefficient. For the simple example considered here, 40 integers (including 0) have to be checked for finding the solution, which is 39. This is an exponential time algorithm, as the size of the input is, up to a constant factor, the number of digits of N, and the average number of operations is of the order of N.

Therefore, this method is rarely used, either for hand-written computation or on computers.

Search by sieving

The search of the solution may be made dramatically faster by sieving. For this method, we suppose, without loss of generality, that \(0\le a_i

\(a_1, a_1 + n_1, a_1+2n_1, \ldots\)

By testing the values of these numbers modulo \(n_2,\) one eventually finds a solution \(x_2\) of the two first congruences. Then the solution belongs to the arithmetic progression

\(x_2, x_2 + n_1n_2, x_2+2n_1n_2, \ldots\)

Testing the values of these numbers modulo \(n_3,\) and continuing until every modulus has been tested eventually yields the solution.

This method is faster if the moduli have been ordered by decreasing value, that is if \(n_1>n_2> \cdots > n_k.\) For the example, this gives the following computation. We consider first the numbers that are congruent to 4 modulo 5 (the largest modulus), which are 4, 9 = 4 + 5, 14 = 9 + 5, ... For each of them, compute the remainder by 4 (the second largest modulus) until getting a number congruent to 3 modulo 4. Then one can proceed by adding 20 = 5 × 4 at each step, and computing only the remainders by 3. This gives

4 mod 4 → 0. Continue

4 + 5 = 9 mod 4 →1. Continue

9 + 5 = 14 mod 4 → 2. Continue

14 + 5 = 19 mod 4 → 3. OK, continue by considering remainders modulo 3 and adding 5 × 4 = 20 each time

19 mod 3 → 1. Continue

19 + 20 = 39 mod 3 → 0. OK, this is the result.

This method works well for hand-written computation with a product of moduli that is not too big. However, it is much slower than other methods, for very large products of moduli. Although dramatically faster than the systematic search, this method also has an exponential time complexity and is therefore not used on computers.

Using the existence construction

The constructive existence proof shows that, in the case of two moduli, the solution may be obtained by the computation of the Bézout coefficients of the moduli, followed by a few multiplications, additions and reductions modulo \(n_1n_2\) (for getting a result in the interval \((0, n_1n_2-1)\)). As the Bézout's coefficients may be computed with the extended Euclidean algorithm, the whole computation, at most, has a quadratic time complexity of \(O((s_1+s_2)^2),\) where \(s_i\) denotes the number of digits of \(n_i.\)

For more than two moduli, the method for two moduli allows the replacement of any two congruences by a single congruence modulo the product of the moduli. Iterating this process provides eventually the solution with a complexity, which is quadratic in the number of digits of the product of all moduli. This quadratic time complexity does not depend on the order in which the moduli are regrouped. One may regroup the two first moduli, then regroup the resulting modulus with the next one, and so on. This strategy is the easiest to implement, but it also requires more computation involving large numbers.

Another strategy consists in partitioning the moduli in pairs whose product have comparable sizes (as much as possible), applying, in parallel, the method of two moduli to each pair, and iterating with a number of moduli approximatively divided by two. This method allows an easy parallelization of the algorithm. Also, if fast algorithms (that is, algorithms working in quasilinear time) are used for the basic operations, this method provides an algorithm for the whole computation that works in quasilinear time.

On the current example (which has only three moduli), both strategies are identical and work as follows.

Bézout's identity for 3 and 4 is

\(1\times 4 + (-1)\times 3 = 1.\)

Putting this in the formula given for proving the existence gives

\(0\times 1\times 4 + 3\times (-1)\times 3 =-9\)

for a solution of the two first congruences, the other solutions being obtained by adding to −9 any multiple of 3 × 4 = 12. One may continue with any of these solutions, but the solution 3 = −9 +12 is smaller (in absolute value) and thus leads probably to an easier computation

\(5\times 5 +(-2)\times 12 =1.\)

\(5\times 5 \times 3 + 12\times (-2)\times 4 = -21.\)

Condensed: the full section is in Wikipedia.

As a linear Diophantine system

The system of congruences solved by the Chinese remainder theorem may be rewritten as a system of linear Diophantine equations:

\(\begin{align} x &= a_1 +x_1n_1\\ &\vdots \\ x &=a_k+x_kn_k, \end{align}\)

where the unknown integers are \(x\) and the \(x_i.\) Therefore, every general method for solving such systems may be used for finding the solution of Chinese remainder theorem, such as the reduction of the matrix of the system to Smith normal form or Hermite normal form. However, as usual when using a general algorithm for a more specific problem, this approach is less efficient than the method of the preceding section, based on a direct use of Bézout's identity.

Over principal ideal domains

In § Statement, the Chinese remainder theorem has been stated in three different ways: in terms of remainders, of congruences, and of a ring isomorphism. The statement in terms of remainders does not apply, in general, to principal ideal domains, as remainders are not defined in such rings. However, the two other versions make sense over a principal ideal domain R: it suffices to replace "integer" by "element of the domain" and \(\mathbb Z\) by R. These two versions of the theorem are true in this context, because the proofs (except for the first existence proof), are based on Euclid's lemma and Bézout's identity, which are true over every principal domain.

However, in general, the theorem is only an existence theorem and does not provide any way for computing the solution, unless one has an algorithm for computing the coefficients of Bézout's identity.

Most te. Nincs számológép rendezi ezt, de a darabjai komposztálhatóak. Próbálja ki az egyiket lentebb, vagy gépelje be a sajátját.

Dolgozz tovább!

Egy ingyenes fiók jegyzeteket ad minden leckéről, egy feljegyzést arról, hogy mit végeztél el, a megoldott problémáidról egy helyen, és egy oktatót, akit megkérdezhetsz erről az oldalról. A matematika maga mindenki számára nyitva áll, bejelentkezve vagy sem.

Regisztrálj! Bejelentkezés

Itt használt szimbólumok

Koppintson a teljes definícióra, a képre és arra, hogy mit jelent minden betű.

Kérdések, amiket az emberek feltesznek

Why are primes so important?

Every integer factors into primes in exactly one way, so primes are the atoms of multiplication. Cryptography relies on that factoring being easy to state and hard to do.

How do I tell whether a big number is prime?

Trial division up to the square root works for small numbers. For large ones, probabilistic tests (Miller-Rabin) give an answer that is wrong with negligible probability, and deterministic tests (AKS) exist but are slower.

Az oldal részei adaptálva Wikipedia (CC BY-SA 4.0). Össze van zavarodva és újra megmagyarázva; a hibák a miénk.

Még több Number Theory