费马素性测试的实现
谁愿意帮我做作业?
我尝试使用 BigIntegers 在 Java 中实现 Fermat 素性测试。我的实现如下,但不幸的是它不起作用。有什么想法吗?
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 的新手。
谢谢!
Who wants to help me with my homework?
I'm try to implement Fermat's primality test in Java using BigIntegers. My implementation is as follows, but unfortunately it doesn't work. Any ideas?
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;
}
I'm new to BigIntegers.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对特定 BigInteger 构造函数的使用是合理的,但您应该使用 拒绝方法 来选择 fermat基地 A.以下是对类中方法的轻微修改,该类也仅使用一个 Random 对象:
Your use of the particular BigInteger constructor is reasonable, but you should use a rejection method to select a fermat base a. Here is a slight modification of your method in a class which also uses exactly one Random object:
a 应该是“在 (1, n − 1] 范围内随机选择一个”,我并没有真正看到这种情况发生。你可以打印 a 来检查。
至于如何做到这一点:
例如,你不应该使用 ;这只是随机性的来源,但 a 应该是一个随机值。
带有 Random 参数的 BigInteger 构造函数 来自 nextInt 的定义,给定参数 k,返回一个随机值 0 到 k-1(包括 k-1),并且您希望 a 大于 1 且小于 n。
a should be "pick a randomly in the range (1, n − 1]" and I don't really see that happening. You could print a to check that.
As for how to do that:
e.g. You shouldn't use the BigInteger constructor with a Random argument; that's just a source of randomness, but a should be a random value.
The 'random.nextInt(n-1)+1' comes from the definition of nextInt which, given argument k, returns a random value 0 up to and including k-1. And you want a to be larger than 1 and smaller than n.