-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest2.java
40 lines (31 loc) · 934 Bytes
/
test2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Now lets see an example code of abstraction in java
import java.math.BigInteger;
class test2 {
public static int solveCRT(int[] residues, int[] moduli) {
int M = 1;
for (int modulus : moduli) {
M *= modulus;
}
int result = 0;
for (int i = 0; i < residues.length; i++) {
int Mi = M / moduli[i];
int MiInverse = findInverse(Mi, moduli[i]);
result += residues[i] * Mi * MiInverse;
}
return result % M;
}
public static int findInverse(int a, int m) {
a = a % m;
for (int i = 1; i < m; i++) {
if ((a * i) % m == 0) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] residues = { 2, 3, 2 };
int[] moduli = { 3, 5, 7 };
System.out.println("x = " + solveCRT(residues, moduli));
}
}