File tree 1 file changed +19
-8
lines changed
src/main/java/com/thealgorithms/maths 1 file changed +19
-8
lines changed Original file line number Diff line number Diff line change 1
1
package remainder ;
2
2
3
- import java .util .* ;
3
+ import java .util .Arrays ;
4
4
5
5
public class ChineseRemainderTheorem {
6
6
7
7
// Helper function to compute the greatest common divisor (GCD)
8
8
public static int gcd (int a , int b ) {
9
- if (b == 0 ) return a ;
9
+ if (b == 0 ) {
10
+ return a ;
11
+ }
10
12
return gcd (b , a % b );
11
13
}
12
14
13
15
// Helper function to compute the modular inverse of a modulo m
14
16
// Uses the extended Euclidean algorithm
15
17
public static int modInverse (int a , int m ) {
16
- int m0 = m , t , q ;
17
- int x0 = 0 , x1 = 1 ;
18
-
19
- if (m == 1 ) return 0 ;
18
+ int m0 = m ;
19
+ int t ;
20
+ int q ;
21
+ int x0 = 0 ;
22
+ int x1 = 1 ;
23
+
24
+ if (m == 1 ) {
25
+ return 0 ;
26
+ }
20
27
21
28
while (a > 1 ) {
22
29
// q is quotient
@@ -33,7 +40,9 @@ public static int modInverse(int a, int m) {
33
40
}
34
41
35
42
// Make x1 positive
36
- if (x1 < 0 ) x1 += m0 ;
43
+ if (x1 < 0 ) {
44
+ x1 += m0 ;
45
+ }
37
46
38
47
return x1 ;
39
48
}
@@ -43,7 +52,9 @@ public static int modInverse(int a, int m) {
43
52
public static int findMinX (int [] num , int [] rem , int k ) {
44
53
// Compute product of all numbers
45
54
int prod = 1 ;
46
- for (int i = 0 ; i < k ; i ++) prod *= num [i ];
55
+ for (int i = 0 ; i < k ; i ++) {
56
+ prod *= num [i ];
57
+ }
47
58
48
59
// Initialize result
49
60
int result = 0 ;
You can’t perform that action at this time.
0 commit comments