Skip to content

Commit cecc449

Browse files
committed
Implemented chinese remainder theorem with test file
1 parent 169a01e commit cecc449

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @brief Implementation of the Chinese Remainder Theorem (CRT) algorithm
7+
* @details
8+
* The Chinese Remainder Theorem (CRT) is used to solve systems of
9+
* simultaneous congruences. Given several pairwise coprime moduli
10+
* and corresponding remainders, the algorithm finds the smallest
11+
* positive solution.
12+
*/
13+
public final class ChineseRemainderTheorem {
14+
private ChineseRemainderTheorem() {
15+
}
16+
17+
/**
18+
* @brief Solves the Chinese Remainder Theorem problem.
19+
* @param remainders The list of remainders.
20+
* @param moduli The list of pairwise coprime moduli.
21+
* @return The smallest positive solution that satisfies all the given congruences.
22+
*/
23+
public static int solveCRT(List<Integer> remainders, List<Integer> moduli) {
24+
int product = 1;
25+
int result = 0;
26+
27+
// Calculate the product of all moduli
28+
for (int mod : moduli) {
29+
product *= mod;
30+
}
31+
32+
// Apply the formula for each congruence
33+
for (int i = 0; i < moduli.size(); i++) {
34+
int partialProduct = product / moduli.get(i);
35+
int inverse = modInverse(partialProduct, moduli.get(i));
36+
result += remainders.get(i) * partialProduct * inverse;
37+
}
38+
39+
// The result should be modulo product to find the smallest positive solution
40+
return result % product;
41+
}
42+
43+
/**
44+
* @brief Computes the modular inverse of a number with respect to a modulus using
45+
* the Extended Euclidean Algorithm.
46+
* @param a The number for which to find the inverse.
47+
* @param m The modulus.
48+
* @return The modular inverse of a modulo m.
49+
*/
50+
private static int modInverse(int a, int m) {
51+
int m0 = m, x0 = 0, x1 = 1;
52+
53+
if (m == 1) return 0;
54+
55+
while (a > 1) {
56+
int q = a / m;
57+
int t = m;
58+
59+
// m is remainder now, process same as Euclid's algorithm
60+
m = a % m;
61+
a = t;
62+
t = x0;
63+
64+
x0 = x1 - q * x0;
65+
x1 = t;
66+
}
67+
68+
// Make x1 positive
69+
if (x1 < 0) {
70+
x1 += m0;
71+
}
72+
73+
return x1;
74+
}
75+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @brief Implementation of the Chinese Remainder Theorem (CRT) algorithm
7+
* @details
8+
* The Chinese Remainder Theorem (CRT) is used to solve systems of
9+
* simultaneous congruences. Given several pairwise coprime moduli
10+
* and corresponding remainders, the algorithm finds the smallest
11+
* positive solution.
12+
*/
13+
public final class ChineseRemainderTheorem {
14+
private ChineseRemainderTheorem() {
15+
}
16+
17+
/**
18+
* @brief Solves the Chinese Remainder Theorem problem.
19+
* @param remainders The list of remainders.
20+
* @param moduli The list of pairwise coprime moduli.
21+
* @return The smallest positive solution that satisfies all the given congruences.
22+
*/
23+
public static int solveCRT(List<Integer> remainders, List<Integer> moduli) {
24+
int product = 1;
25+
int result = 0;
26+
27+
// Calculate the product of all moduli
28+
for (int mod : moduli) {
29+
product *= mod;
30+
}
31+
32+
// Apply the formula for each congruence
33+
for (int i = 0; i < moduli.size(); i++) {
34+
int partialProduct = product / moduli.get(i);
35+
int inverse = modInverse(partialProduct, moduli.get(i));
36+
result += remainders.get(i) * partialProduct * inverse;
37+
}
38+
39+
// The result should be modulo product to find the smallest positive solution
40+
return result % product;
41+
}
42+
43+
/**
44+
* @brief Computes the modular inverse of a number with respect to a modulus using
45+
* the Extended Euclidean Algorithm.
46+
* @param a The number for which to find the inverse.
47+
* @param m The modulus.
48+
* @return The modular inverse of a modulo m.
49+
*/
50+
private static int modInverse(int a, int m) {
51+
int m0 = m, x0 = 0, x1 = 1;
52+
53+
if (m == 1) return 0;
54+
55+
while (a > 1) {
56+
int q = a / m;
57+
int t = m;
58+
59+
// m is remainder now, process same as Euclid's algorithm
60+
m = a % m;
61+
a = t;
62+
t = x0;
63+
64+
x0 = x1 - q * x0;
65+
x1 = t;
66+
}
67+
68+
// Make x1 positive
69+
if (x1 < 0) {
70+
x1 += m0;
71+
}
72+
73+
return x1;
74+
}
75+
}

0 commit comments

Comments
 (0)