-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchinese_remainder_theorem.java
More file actions
45 lines (36 loc) · 1008 Bytes
/
chinese_remainder_theorem.java
File metadata and controls
45 lines (36 loc) · 1008 Bytes
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
41
42
43
44
45
// To find the smallest number x such that:
// x % a1 = r1
// x % a2 = r2
// ...
// x % ak = rk
public class chinese_remainder_theorem{
public static int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a % b);
}
public static int modInverse(int a, int m){
a = a % m;
for(int x = 1; x < m; x++){
if((a * x) % m == 1) return x;
}
return -1;
}
public static int chineseRemainderTheorem(int[] a, int[] r, int k){
int prod = 1;
for(int i=0; i<k; i++){
prod *= a[i];
}
int result = 0;
for(int i=0; i<k; i++){
int pp = prod / a[i];
result += r[i] * modInverse(pp, a[i]) * pp;
}
return result % prod;
}
public static void main(String[] args) {
int[] a = {3,5,7};
int[] r = {2,3,2};
int k = a.length;
System.out.println("The smallest number is " + chineseRemainderTheorem(a, r, k));
}
}