Suppose , , , are positive [[integer]]s that are pairwise co-prime.
Then, for any given sequence of integers , , , , there exists an integer solving the following system of simultaneous congruences:
:::
Furthermore, all solutions of this system are congruent modulo the product, .
;Task:
Write a program to solve a system of linear congruences by applying the [[wp:Chinese Remainder Theorem|Chinese Remainder Theorem]].
If the system of equations cannot be solved, your program must somehow indicate this.
(It may throw an exception or return a special false value.)
Since there are infinitely many solutions, the program should return the unique solution where .
''Show the functionality of this program'' by printing the result such that the 's are and the 's are .
'''Algorithm''': The following algorithm only applies if the 's are pairwise co-prime.
Suppose, as above, that a solution is required for the system of congruences:
:::
Again, to begin, the product is defined.
Then a solution can be found as follows:
For each , the integers and are co-prime.
Using the [[wp:Extended Euclidean algorithm|Extended Euclidean algorithm]], we can find integers and such that .
Then, one solution to the system of simultaneous congruences is:
:::
and the minimal solution,
::: .