Skip to content

Commit 3b2ba48

Browse files
authored
Add chinese remainder theorem (#5873)
1 parent e154a50 commit 3b2ba48

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
// Adjust result to be the smallest positive solution
40+
result = result % product;
41+
if (result < 0) {
42+
result += product;
43+
}
44+
45+
return result;
46+
}
47+
48+
/**
49+
* @brief Computes the modular inverse of a number with respect to a modulus using
50+
* the Extended Euclidean Algorithm.
51+
* @param a The number for which to find the inverse.
52+
* @param m The modulus.
53+
* @return The modular inverse of a modulo m.
54+
*/
55+
private static int modInverse(int a, int m) {
56+
int m0 = m;
57+
int x0 = 0;
58+
int x1 = 1;
59+
60+
if (m == 1) {
61+
return 0;
62+
}
63+
64+
while (a > 1) {
65+
int q = a / m;
66+
int t = m;
67+
68+
// m is remainder now, process same as Euclid's algorithm
69+
m = a % m;
70+
a = t;
71+
t = x0;
72+
73+
x0 = x1 - q * x0;
74+
x1 = t;
75+
}
76+
77+
// Make x1 positive
78+
if (x1 < 0) {
79+
x1 += m0;
80+
}
81+
82+
return x1;
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class ChineseRemainderTheoremTest {
10+
@Test
11+
public void testCRTSimpleCase() {
12+
List<Integer> remainders = Arrays.asList(2, 3, 2);
13+
List<Integer> moduli = Arrays.asList(3, 5, 7);
14+
int expected = 23;
15+
int result = ChineseRemainderTheorem.solveCRT(remainders, moduli);
16+
assertEquals(expected, result);
17+
}
18+
19+
@Test
20+
public void testCRTLargeModuli() {
21+
List<Integer> remainders = Arrays.asList(1, 2, 3);
22+
List<Integer> moduli = Arrays.asList(5, 7, 9);
23+
int expected = 156;
24+
int result = ChineseRemainderTheorem.solveCRT(remainders, moduli);
25+
assertEquals(expected, result);
26+
}
27+
28+
@Test
29+
public void testCRTWithSingleCongruence() {
30+
List<Integer> remainders = Arrays.asList(4);
31+
List<Integer> moduli = Arrays.asList(7);
32+
int expected = 4;
33+
int result = ChineseRemainderTheorem.solveCRT(remainders, moduli);
34+
assertEquals(expected, result);
35+
}
36+
37+
@Test
38+
public void testCRTWithMultipleSolutions() {
39+
List<Integer> remainders = Arrays.asList(0, 3);
40+
List<Integer> moduli = Arrays.asList(4, 5);
41+
int expected = 8;
42+
int result = ChineseRemainderTheorem.solveCRT(remainders, moduli);
43+
assertEquals(expected, result);
44+
}
45+
46+
@Test
47+
public void testCRTLargeNumbers() {
48+
List<Integer> remainders = Arrays.asList(0, 4, 6);
49+
List<Integer> moduli = Arrays.asList(11, 13, 17);
50+
int expected = 550;
51+
int result = ChineseRemainderTheorem.solveCRT(remainders, moduli);
52+
assertEquals(expected, result);
53+
}
54+
}

0 commit comments

Comments
 (0)