feat: mod inv
This commit is contained in:
@@ -26,13 +26,16 @@ public class ModularInv {
|
|||||||
for (long j = 1; j < 10000; j++) {
|
for (long j = 1; j < 10000; j++) {
|
||||||
BigInteger a = BigInteger.valueOf(i);
|
BigInteger a = BigInteger.valueOf(i);
|
||||||
BigInteger p = BigInteger.valueOf(j);
|
BigInteger p = BigInteger.valueOf(j);
|
||||||
|
// gcd(a, p) = 1
|
||||||
if (!a.gcd(p).equals(BigInteger.ONE)) {
|
if (!a.gcd(p).equals(BigInteger.ONE)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// a^-1 mod p
|
||||||
BigInteger x = a.modInverse(p);
|
BigInteger x = a.modInverse(p);
|
||||||
BigInteger y = modularInv(a, p);
|
BigInteger y = modularInv(a, p);
|
||||||
System.out.println(Arrays.asList(a, p, a.modInverse(p), modularInv(a, p)));
|
BigInteger z = a.modPow(Phi.phi(p).subtract(BigInteger.ONE), p);
|
||||||
if (!y.equals(x)) {
|
System.out.println(Arrays.asList(a + "^-1 mod " + p, x, y, z));
|
||||||
|
if (!y.equals(x) || !y.equals(z)) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user