-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChineseRemainderTheorem.java
More file actions
71 lines (58 loc) · 1.86 KB
/
ChineseRemainderTheorem.java
File metadata and controls
71 lines (58 loc) · 1.86 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.Scanner;
public class ChineseRemainderTheorem {
public static int gcdExtended(int a, int b, int[] x, int[] y) {
if (a == 0) {
x[0] = 0;
y[0] = 1;
return b;
}
int[] x1 = new int[1];
int[] y1 = new int[1];
int gcd = gcdExtended(b % a, a, x1, y1);
x[0] = y1[0] - (b / a) * x1[0];
y[0] = x1[0];
return gcd;
}
public static int modInverse(int a, int m) {
int[] x = new int[1];
int[] y = new int[1];
int g = gcdExtended(a, m, x, y);
if (g != 1) {
throw new ArithmeticException("Inverse doesn't exist");
} else {
return (x[0] % m + m) % m;
}
}
public static int chineseRemainderTheorem(int[] a, int[] m) {
int prod = 1;
for (int i : m) {
prod *= i;
}
int result = 0;
for (int i = 0; i < a.length; i++) {
int partialProd = prod / m[i];
result += a[i] * modInverse(partialProd, m[i]) * partialProd;
}
return result % prod;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of equations: ");
int n = scanner.nextInt();
int[] a = new int[n];
int[] m = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter remainder a[" + i + "]: ");
a[i] = scanner.nextInt();
System.out.print("Enter modulus m[" + i + "]: ");
m[i] = scanner.nextInt();
}
try {
int result = chineseRemainderTheorem(a, m);
System.out.println("The solution is x = " + result);
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
scanner.close();
}
}