forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModularInverse.java
More file actions
51 lines (46 loc) · 1.42 KB
/
ModularInverse.java
File metadata and controls
51 lines (46 loc) · 1.42 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
/**
* Computes the modular inverse of a number using the Extended Euclidean Algorithm.
*
* <p>The modular inverse of 'a' mod 'm' is a value x such that a*x ≡ 1 (mod m). It exists if and
* only if gcd(a, m) = 1 (i.e. a and m are coprime).
*
* <p>Time: ~O(log(a + m))
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.math;
public class ModularInverse {
/**
* Returns the modular inverse of 'a' mod 'm', or null if it does not exist.
*
* @param a the value to invert.
* @param m the modulus (must be positive).
* @return the modular inverse, or null if gcd(a, m) != 1.
* @throws ArithmeticException if m is not positive.
*/
public static Long modInv(long a, long m) {
if (m <= 0)
throw new ArithmeticException("mod must be > 0");
a = ((a % m) + m) % m;
long[] v = egcd(a, m);
if (v[0] != 1)
return null;
return ((v[1] % m) + m) % m;
}
// Returns [gcd(a, b), x, y] such that ax + by = gcd(a, b).
private static long[] egcd(long a, long b) {
if (b == 0)
return new long[] {a, 1, 0};
long[] v = egcd(b, a % b);
long tmp = v[1] - v[2] * (a / b);
v[1] = v[2];
v[2] = tmp;
return v;
}
public static void main(String[] args) {
// 2*3 mod 5 = 1, so modInv(2, 5) = 3.
System.out.println(modInv(2, 5));
// gcd(4, 18) != 1, so no inverse exists.
System.out.println(modInv(4, 18));
}
}