Ai
1 Star 0 Fork 3

aiobc/Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ModularInverse.java 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
Rafael Chaves 提交于 2020-02-03 02:07 +08:00 . Adopt standard Java project layout (#120)
/** Time Complexity ~O(log(a + b)) */
package com.williamfiset.algorithms.math;
public class ModularInverse {
// This function performs the extended euclidean algorithm on two numbers a and b.
// The function returns the gcd(a,b) as well as the numbers x and y such
// that ax + by = gcd(a,b). This calculation is important in number theory
// and can be used for several things such as finding modular inverses and
// solutions to linear Diophantine equations.
private static long[] egcd(long a, long b) {
if (b == 0) return new long[] {a, 1L, 0L};
long[] v = egcd(b, a % b);
long tmp = v[1] - v[2] * (a / b);
v[1] = v[2];
v[2] = tmp;
return v;
}
// Returns the modular inverse of 'a' mod 'm' if it exists.
// Make sure m > 0 and 'a' & 'm' are relatively prime.
public static Long modInv(long a, long m) {
if (m <= 0) throw new ArithmeticException("mod must be > 0");
// Avoid a being negative
a = ((a % m) + m) % m;
long[] v = egcd(a, m);
long gcd = v[0];
long x = v[1];
if (gcd != 1) return null;
return ((x + m) % m) % m;
}
public static void main(String[] args) {
// Prints 3 since 2*3 mod 5 = 1
System.out.println(modInv(2, 5));
// Prints null because there is no
// modular inverse such that 4*x mod 18 = 1
System.out.println(modInv(4, 18));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aiobc/Algorithms.git
git@gitee.com:aiobc/Algorithms.git
aiobc
Algorithms
Algorithms
master

搜索帮助