【发布时间】:2011-04-30 22:40:55
【问题描述】:
谁愿意帮我做作业?
我正在尝试使用 BigIntegers 在 Java 中实现 Fermat's primality test。我的实现如下,但不幸的是它不起作用。有什么想法吗?
public static boolean checkPrime(BigInteger n, int maxIterations)
{
if (n.equals(BigInteger.ONE))
return false;
BigInteger a;
Random rand = new Random();
for (int i = 0; i < maxIterations; i++)
{
a = new BigInteger(n.bitLength() - 1, rand);
a = a.modPow(n.subtract(BigInteger.ONE), n);
if (!a.equals(BigInteger.ONE))
return false;
}
return true;
}
我是 BigIntegers 的新手。
谢谢!
【问题讨论】:
-
如果你能说出“不起作用”的样子,你会有更好的运气。
-
“不起作用”是指它没有给出正确的结果。例如,当我针对 2、3、5 和 7 等素数测试它时,它应该返回“true”时返回“false”。
-
你检查过 a = new BigInteger(n.bitLength() - 1, rand);确实是打印时的正常值,并且 rand 从 1 正确初始化为 n (我没有看到那部分)?
标签: java math primes biginteger