How to resolve the algorithm Chinese remainder theorem step by step in the JavaScript programming language
How to resolve the algorithm Chinese remainder theorem step by step in the JavaScript programming language
Table of Contents
Problem Statement
Suppose
n
1
{\displaystyle n_{1}}
,
n
2
{\displaystyle n_{2}}
,
…
{\displaystyle \ldots }
,
n
k
{\displaystyle n_{k}}
are positive integers that are pairwise co-prime. Then, for any given sequence of integers
a
1
{\displaystyle a_{1}}
,
a
2
{\displaystyle a_{2}}
,
…
{\displaystyle \dots }
,
a
k
{\displaystyle a_{k}}
, there exists an integer
x
{\displaystyle x}
solving the following system of simultaneous congruences: Furthermore, all solutions
x
{\displaystyle x}
of this system are congruent modulo the product,
N
n
1
n
2
…
n
k
{\displaystyle N=n_{1}n_{2}\ldots n_{k}}
.
Write a program to solve a system of linear congruences by applying the 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
{\displaystyle s}
where
0 ≤ s ≤
n
1
n
2
…
n
k
{\displaystyle 0\leq s\leq n_{1}n_{2}\ldots n_{k}}
.
Show the functionality of this program by printing the result such that the
n
{\displaystyle n}
's are
[ 3 , 5 , 7 ]
{\displaystyle [3,5,7]}
and the
a
{\displaystyle a}
's are
[ 2 , 3 , 2 ]
{\displaystyle [2,3,2]}
.
Algorithm: The following algorithm only applies if the
n
i
{\displaystyle n_{i}}
's are pairwise co-prime. Suppose, as above, that a solution is required for the system of congruences: Again, to begin, the product
N
n
1
n
2
…
n
k
{\displaystyle N=n_{1}n_{2}\ldots n_{k}}
is defined. Then a solution
x
{\displaystyle x}
can be found as follows: For each
i
{\displaystyle i}
, the integers
n
i
{\displaystyle n_{i}}
and
N
/
n
i
{\displaystyle N/n_{i}}
are co-prime. Using the Extended Euclidean algorithm, we can find integers
r
i
{\displaystyle r_{i}}
and
s
i
{\displaystyle s_{i}}
such that
r
i
n
i
s
i
N
/
n
i
= 1
{\displaystyle r_{i}n_{i}+s_{i}N/n_{i}=1}
. Then, one solution to the system of simultaneous congruences is: and the minimal solution,
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Chinese remainder theorem step by step in the JavaScript programming language
The provided JavaScript code implements the Chinese Remainder Theorem (CRT) to solve a system of linear congruences of the form:
x ≡ r1 (mod m1)
x ≡ r2 (mod m2)
...
x ≡ rn (mod mn)
Here's a detailed explanation of the code:
-
crt
Function:- Takes two arrays as input:
num
containing the moduli(m1, m2, ..., mn)
andrem
containing the remainders(r1, r2, ..., rn)
. - Initializes a variable
sum
to 0. - Calculates the product of all moduli in
num
and stores it inprod
. - Iterates over each modulus
ni
and remainderri
in the arrays. - Calculates
p
as the floor division ofprod
byni
. - Computes the modular inverse of
p
moduloni
using themulInv
function and multiplies it byri
. - Adds the result to
sum
. - Finally, returns
sum % prod
.
- Takes two arrays as input:
-
mulInv
Function:- Takes two integers
a
andb
as input. - Initializes
b0
with the value ofb
. - Initializes
x0
andx1
to 0 and 1, respectively. - If
b
is 1, it returns 1 since the inverse is simply 1 modulo itself. - Otherwise, it enters a loop that continues until
a
becomes less than or equal to 1. - Inside the loop, it calculates the quotient
q
as the floor division ofa
byb
, updatesa
andb
tob
anda % b
, respectively, and updatesx0
andx1
tox1 - q * x0
andx0
, respectively. - After the loop, it checks if
x1
is negative. If so, it addsb0
to it to make it positive. - Finally, it returns
x1
.
- Takes two integers
Example Usage:
The code you provided includes an example usage where it calls the crt
function with the arrays num
and rem
and logs the result to the console:
console.log(crt([3, 5, 7], [2, 3, 2]));
This particular example solves the system of linear congruences:
x ≡ 2 (mod 3)
x ≡ 3 (mod 5)
x ≡ 2 (mod 7)
and returns the solution x = 23
, which satisfies all three congruences.
Source code in the javascript programming language
function crt(num, rem) {
let sum = 0;
const prod = num.reduce((a, c) => a * c, 1);
for (let i = 0; i < num.length; i++) {
const [ni, ri] = [num[i], rem[i]];
const p = Math.floor(prod / ni);
sum += ri * p * mulInv(p, ni);
}
return sum % prod;
}
function mulInv(a, b) {
const b0 = b;
let [x0, x1] = [0, 1];
if (b === 1) {
return 1;
}
while (a > 1) {
const q = Math.floor(a / b);
[a, b] = [b, a % b];
[x0, x1] = [x1 - q * x0, x0];
}
if (x1 < 0) {
x1 += b0;
}
return x1;
}
console.log(crt([3,5,7], [2,3,2]))
You may also check:How to resolve the algorithm Ascending primes step by step in the Fortran programming language
You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the C# programming language
You may also check:How to resolve the algorithm Polymorphic copy step by step in the Delphi programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Grayscale image step by step in the J programming language