0% found this document useful (0 votes)
6 views3 pages

Java Implementation of CRT

The Chinese Remainder Theorem provides a method to find a unique solution to a system of simultaneous linear congruences with pairwise coprime moduli. It states that if you have congruences of the form x ≡ a1 (mod m1), x ≡ a2 (mod m2), etc., where the moduli m1, m2, etc. are coprime, there exists a unique solution x modulo M = m1*m2*...*mn. This solution can be found as x ≡ (a1*M1*y1 + a2*M2*y2 + ... + an*Mn*yn) (mod M) where Mi = M/mi

Uploaded by

amitrj850
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Implementation of CRT

The Chinese Remainder Theorem provides a method to find a unique solution to a system of simultaneous linear congruences with pairwise coprime moduli. It states that if you have congruences of the form x ≡ a1 (mod m1), x ≡ a2 (mod m2), etc., where the moduli m1, m2, etc. are coprime, there exists a unique solution x modulo M = m1*m2*...*mn. This solution can be found as x ≡ (a1*M1*y1 + a2*M2*y2 + ... + an*Mn*yn) (mod M) where Mi = M/mi

Uploaded by

amitrj850
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXPERIMENT-10

Aim: Chinese Remainder Theorem

To solve a system of congruences efficiently. Solving systems of congruences with pairwise


coprime moduli.

Theory:
The Chinese Remainder Theorem (CRT) is a fundamental theorem in number theory and
modular arithmetic. It provides a method for finding a unique solution to a system of
simultaneous linear congruences with pairwise coprime moduli.
Formally, the Chinese Remainder Theorem states the
following: Given a system of simultaneous linear
congruences:
x ≡ a₁
(mod m₁)
x ≡ a₂
(mod m₂)
...
x ≡ aₙ (mod mₙ)
where a₁, a₂, ..., aₙ are integers, and m₁, m₂, ..., mₙ are pairwise coprime positive integers
(i.e., they have no common factors other than 1), then there exists a unique solution for x
modulo
M, where M = m₁ * m₂ * ... * mₙ.
Moreover, the unique solution x modulo M can be found as:
x ≡ (a₁ * M₁ * y₁ + a₂ * M₂ * y₂ + ... + aₙ * Mₙ * yₙ) (mod
M) where Mᵢ = M / mᵢ and yᵢ is the modular inverse of Mᵢ
modulo mᵢ.

Source Code:
import [Link];
public class ChineseRemainderTheorem
public static BigInteger modInverse(BigInteger a, BigInteger m) {
BigInteger m0 = m;
BigInteger x0 =
[Link]; BigInteger
x1 = [Link]; while
([Link]([Link]) >
0)
BigInteger q = [Link](m);
BigInteger t = m;

m=
[Link](m);
a = t;
t = x0;
x0 =
[Link]([Link](x0));
x1 = t;
}
if ([Link]([Link])
< 0) { x1 = [Link](m0);
}

return x1;
}

public static BigInteger chineseRemainderTheorem(BigInteger[] num, BigInteger[] rem) {


if ([Link] != [Link]) {
throw new IllegalArgumentException("Input arrays must have the same length");
}

BigInteger prod = [Link];


for (BigInteger n : num) {
prod = [Link](n);
}

BigInteger result = [Link];

for (int i = 0; i < [Link]; i++) {


BigInteger partialProduct = [Link](num[i]);
BigInteger inverse = modInverse(partialProduct, num[i]);
result = [Link](rem[i].multiply(partialProduct).multiply(inverse));
}

return [Link](prod);
}

public static void main(String[] args) {

BigInteger[] num = {[Link](3), [Link](4),


[Link](5)};
BigInteger[] rem = {[Link](2), [Link](3),
[Link](1)};

BigInteger result = chineseRemainderTheorem(num, rem);


[Link]("The solution is: " + result);
}
}
Output:

SUBMITTED BY :-

NAME : RATNAPRAVA SAHOO

REGN : 2201030074

GROUP : 08

BRANCH : CSE(AI&ML)

Common questions

Powered by AI

The Chinese Remainder Theorem ensures a unique solution to a system of congruences by requiring that the moduli are pairwise coprime. Under this condition, the theorem states that there exists a unique solution for x modulo the product of the moduli. This is achieved by finding each term of the solution with respect to its own modulus and then constructing the solution using a linear combination of these terms, weighted by their respective modular inverses, ensuring compatibility across all congruences .

The modular inverse required for the application of the Chinese Remainder Theorem can be found using the extended Euclidean algorithm. In the provided Java source code, this involves iterating through steps to update the modular inverse using a series of calculations involving division and subtraction. This process ensures that the value is adjusted until it satisfies the necessary condition of being the inverse .

Pairwise coprimality in the moduli is significant because it guarantees the independence of each congruence relation in the system. This allows the construction of a unified solution that satisfies all congruences simultaneously. If the moduli are not pairwise coprime, there can be multiple solutions or no consistent solution, which undermines the efficacy and applicability of the Chinese Remainder Theorem .

The Java code handles potential negative results from the modular inverse computation by checking if the final result 'x1' is negative. If so, it adjusts the result by adding the modulus to bring it into the non-negative range corresponding to the modulus. This ensures the modular inverse is correctly situated within the expected bounds for subsequent calculations .

The Chinese Remainder Theorem (CRT) is fundamental in number theory and modular arithmetic as it provides a systematic method for solving systems of simultaneous linear congruences with pairwise coprime moduli. Specifically, the CRT guarantees the existence of a unique solution modulo the product of the moduli, simplifying complex calculations and finding solutions efficiently in cryptography, computer algebra systems, and multivariate polynomial arithmetic .

In the Java code implementation, the product of all moduli is calculated by iterating over the array of moduli and multiplying them together sequentially. This cumulative product, stored in the variable 'prod', is essential for determining the overall modulus and is used to calculate each partial product and in the final solution formula .

Ensuring the input arrays have the same length is crucial because each residue must correspond exactly to a modulus for the Chinese Remainder Theorem to apply correctly. If the arrays of moduli and residues do not match in length, it indicates an inconsistency or misalignment in the system of congruences to be solved, which would render the calculation of a valid solution impossible .

Modular inverses play a critical role in the Chinese Remainder Theorem as they enable the construction of solutions that are consistent across different moduli. Each modular inverse corresponds to the inverse of a partial product modulo its specific modulus. By multiplying each residue by its corresponding partial product and modular inverse, and summing these terms, a single solution is constructed that satisfies all given congruences when computed modulo the product of all moduli .

If the product of moduli is incorrectly calculated, it could lead to an incorrect determination of partial products and the final combination of terms in the solution. This would result in an invalid solution that does not satisfy the original system of congruences, as the moduli define the structure and scope of the solution within the permissible domain .

The algorithm used to compute the modular inverse in the Java code employs the extended Euclidean algorithm. It iteratively updates two variables, 'x0' and 'x1', which eventually converge to the inverse once the initial number 'a' is reduced to 1 by a series of divisions and substitutions. The result is adjusted to be non-negative by ensuring it lies in the correct range of the modulus .

You might also like