Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Apr 19, 2019
1 parent ef77568 commit 8195394
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion java/numbertheory/Euclid.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public static int mod(int a, int m) {
return a >= 0 ? a : a + m;
}

public static int mod(long a, int m) {
a %= m;
return (int) (a >= 0 ? a : a + m);
}

// precondition: m > 0 && gcd(a, m) = 1
public static int modInverse(int a, int m) {
a = mod(a, m);
Expand All @@ -79,7 +84,7 @@ public static BigInteger garnerRestore(int[] a, int[] p) {
int[] x = a.clone();
for (int i = 0; i < x.length; ++i)
for (int j = 0; j < i; ++j)
x[i] = mod(BigInteger.valueOf(p[j]).modInverse(BigInteger.valueOf(p[i])).intValue() * (x[i] - x[j]), p[i]);
x[i] = mod(BigInteger.valueOf(p[j]).modInverse(BigInteger.valueOf(p[i])).longValue() * (x[i] - x[j]), p[i]);
BigInteger res = BigInteger.valueOf(x[0]);
BigInteger m = BigInteger.ONE;
for (int i = 1; i < x.length; i++) {
Expand Down Expand Up @@ -134,5 +139,7 @@ public static void main(String[] args) {
+ res[0]);

System.out.println(Arrays.toString(generateInverses(7)));

System.out.println(garnerRestore(new int[]{200_000_125, 300_000_333}, new int[]{1000_000_007, 1000_000_009}));
}
}

0 comments on commit 8195394

Please sign in to comment.