【发布时间】:2010-10-26 17:12:52
【问题描述】:
我正在尝试使用 BigInteger 类在 Java 中实现 Fermat、Miller-Rabin 或 AKS 算法。
我想我已经实现了Fermat test,除了 BigInteger 类不允许将 BigIntegers 带到 BigIntegers 的力量(只能将 BigIntegers 带到原始整数的力量)。 有没有办法解决这个问题?
我的代码中指出了有问题的行:
public static boolean fermatPrimalityTest(BigInteger n)
{
BigInteger a;
Random rand = new Random();
int maxIterations = 100000;
for (int i = 0; i < maxIterations; i++) {
a = new BigInteger(2048, rand);
// PROBLEM WITH a.pow(n) BECAUSE n IS NOT A BigInteger
boolean test = ((a.pow(n)).minus(BigInteger.ONE)).equals((BigInteger.ONE).mod(n));
if (!test)
return false;
}
return true;
}
【问题讨论】:
标签: java math primes biginteger