Suppose   n_1,   n_2,   \ldots,   n_k   are positive [[integer]]s that are pairwise co-prime.   Then, for any given sequence of integers   a_1,   a_2,   \dots,   a_k,   there exists an integer   x   solving the following system of simultaneous congruences: ::: \begin{align} x &\equiv a_1 \pmod{n_1} \\ x &\equiv a_2 \pmod{n_2} \\ &{}\ \ \vdots \\ x &\equiv a_k \pmod{n_k} \end{align} Furthermore, all solutions   x   of this system are congruent modulo the product,   N=n_1n_2\ldots n_k. ;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   s   where   0 \leq s \leq n_1n_2\ldots n_k. ''Show the functionality of this program'' by printing the result such that the   n's   are   [3,5,7]   and the   a's   are   [2,3,2]. '''Algorithm''':   The following algorithm only applies if the   n_i's   are pairwise co-prime. Suppose, as above, that a solution is required for the system of congruences: ::: x \equiv a_i \pmod{n_i} \quad\mathrm{for}\; i = 1, \ldots, k Again, to begin, the product   N = n_1n_2 \ldots n_k   is defined. Then a solution   x   can be found as follows: For each   i,   the integers   n_i   and   N/n_i   are co-prime. Using the   [[wp:Extended Euclidean algorithm|Extended Euclidean algorithm]],   we can find integers   r_i   and   s_i   such that   r_i n_i + s_i N/n_i = 1. Then, one solution to the system of simultaneous congruences is: ::: x = \sum_{i=1}^k a_i s_i N/n_i and the minimal solution, ::: x \pmod{N}.