|
1 | 1 | package com.thealgorithms.maths;
|
2 | 2 |
|
3 |
| -import java.util.List; |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 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 ChineseRemainderTheoremTest { |
14 |
| - private ChineseRemainderTheoremTest() { |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +public class ChineseRemainderTheoremTest { |
| 11 | + @Test |
| 12 | + public void testCRTSimpleCase() { |
| 13 | + List<Integer> remainders = Arrays.asList(2, 3, 2); |
| 14 | + List<Integer> moduli = Arrays.asList(3, 5, 7); |
| 15 | + int expected = 23; |
| 16 | + int result = ChineseRemainderTheorem.solveCRT(remainders, moduli); |
| 17 | + assertEquals(expected, result); |
15 | 18 | }
|
16 | 19 |
|
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; |
| 20 | + @Test |
| 21 | + public void testCRTLargeModuli() { |
| 22 | + List<Integer> remainders = Arrays.asList(1, 2, 3); |
| 23 | + List<Integer> moduli = Arrays.asList(5, 7, 9); |
| 24 | + int expected = 131; |
| 25 | + int result = ChineseRemainderTheorem.solveCRT(remainders, moduli); |
| 26 | + assertEquals(expected, result); |
41 | 27 | }
|
42 | 28 |
|
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 |
| - } |
| 29 | + @Test |
| 30 | + public void testCRTWithSingleCongruence() { |
| 31 | + List<Integer> remainders = Arrays.asList(4); |
| 32 | + List<Integer> moduli = Arrays.asList(7); |
| 33 | + int expected = 4; |
| 34 | + int result = ChineseRemainderTheorem.solveCRT(remainders, moduli); |
| 35 | + assertEquals(expected, result); |
| 36 | + } |
67 | 37 |
|
68 |
| - // Make x1 positive |
69 |
| - if (x1 < 0) { |
70 |
| - x1 += m0; |
71 |
| - } |
| 38 | + @Test |
| 39 | + public void testCRTWithMultipleSolutions() { |
| 40 | + List<Integer> remainders = Arrays.asList(0, 3); |
| 41 | + List<Integer> moduli = Arrays.asList(4, 5); |
| 42 | + int expected = 15; |
| 43 | + int result = ChineseRemainderTheorem.solveCRT(remainders, moduli); |
| 44 | + assertEquals(expected, result); |
| 45 | + } |
72 | 46 |
|
73 |
| - return x1; |
| 47 | + @Test |
| 48 | + public void testCRTLargeNumbers() { |
| 49 | + List<Integer> remainders = Arrays.asList(0, 4, 6); |
| 50 | + List<Integer> moduli = Arrays.asList(11, 13, 17); |
| 51 | + int expected = 782; |
| 52 | + int result = ChineseRemainderTheorem.solveCRT(remainders, moduli); |
| 53 | + assertEquals(expected, result); |
74 | 54 | }
|
75 | 55 | }
|
0 commit comments