查找长度至少为 100 位且包含 273042282802155991 的素数
我是 Java 新手,我的课堂作业之一是找到一个长度至少为 100 位的质数,其中包含数字 273042282802155991。
到目前为止,我已经做到了这一点,但当我编译并运行它时,它似乎处于连续循环中。
我不确定我是否做错了什么。
public static void main(String[] args) {
BigInteger y = BigInteger.valueOf(304877713615599127L);
System.out.println(RandomPrime(y));
}
public static BigInteger RandomPrime(BigInteger x)
{
BigInteger i;
for (i = BigInteger.valueOf(2); i.compareTo(x)<0; i.add(i)) {
if ((x.remainder(i).equals(BigInteger.ZERO))) {
x.divide(i).equals(x);
i.subtract(i);
}
}
return i;
}
I am new to Java and one of my class assignments is to find a prime number at least 100 digits long that contains the numbers 273042282802155991.
I have this so far but when I compile it and run it it seems to be in a continuous loop.
I'm not sure if I've done something wrong.
public static void main(String[] args) {
BigInteger y = BigInteger.valueOf(304877713615599127L);
System.out.println(RandomPrime(y));
}
public static BigInteger RandomPrime(BigInteger x)
{
BigInteger i;
for (i = BigInteger.valueOf(2); i.compareTo(x)<0; i.add(i)) {
if ((x.remainder(i).equals(BigInteger.ZERO))) {
x.divide(i).equals(x);
i.subtract(i);
}
}
return i;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为这是家庭作业...
BigInteger 上有一个方法可以测试素数。这比尝试对数字进行因式分解要快得多。 (如果您采用尝试分解 100 位数字的方法,您将会失败。分解被认为是一个 NP 完全问题。当然,不存在已知多项式时间解决方案。)
问题要求提供一个素数,该素数包含给定的数字序列(当它表示为十进制数字序列时)。
生成“随机”素数然后测试它们是否包含这些数字的方法是不可行的。 (一些简单的高中数学告诉你,随机生成的 100 位数字包含给定的 18 位数字序列的概率是 ... 82 / 1018。而且你还没有测试素性...
但是还有另一种方法可以做到这一点...考虑一下!
只有在您在头脑中弄清楚算法将如何工作并进行心理估计以确认它将给出答案后才开始编写代码 的时间长度。
!合理 不可行,我的意思是对你来说不可行。只要有足够多的计算机、足够的时间和一些高能的数学知识,其中一些事情也许是可能的。因此,从技术上讲,它们可能在计算上是可行的。但它们作为家庭作业是不可行的。我确信这个练习的目的是让您思考如何以聪明的方式做到这一点......
Since this is homework ...
There is a method on BigInteger that tests for primality. This is much much faster than attempting to factorize a number. (If you take an approach that involves attempting to factorize 100 digit numbers you will fail. Factorization is believed to be an NP-complete problem. Certainly, there is no known polynomial time solution.)
The question is asking for a prime number that contains a given sequence of digits when it is represented as a sequence of decimal digits.
The approach of generating "random" primes and then testing if they contain those digits is infeasible. (Some simple high-school maths tells you that the probability that a randomly generated 100 digit number contains a given 18 digit sequence is ... 82 / 1018. And you haven't tested for primality yet ...
But there's another way to do it ... think about it!
Only start writing code once you've figured out in your head how your algorithm will work, and done the mental estimates to confirm that it will give an answer in a reasonable length of time.
When I say infeasible, I mean infeasible for you. Given a large enough number of computers, enough time and some high-powered mathematics, it may be possible to do some of these things. Thus, technically they may be computationally feasible. But they are not feasible as a homework exercise. I'm sure that the point of this exercise is to get you to think about how to do this the smart way ...
一个提示是这些语句不执行任何操作:
与
for
循环的一部分相同:它们不会修改实例本身,而是返回新值 - 您无法检查并执行任何操作的值。
BigIntegers
是“不可变的”。它们无法更改 - 但可以对它们进行操作并返回新值。如果你真的想做这样的事情,你必须这样做:
另外,为什么要从
i
中减去i
?你不是一直期望这个值为 0 吗?One tip is that these statements do nothing:
Same with part of your
for
loop:They don't modify the instances themselves, but return new values - values that you're failing to check and do anything with.
BigIntegers
are "immutable". They can't be changed - but they can be operated upon and return new values.If you actually wanted to do something like this, you would have to do:
Also, why would you subtract
i
fromi
? Wouldn't you always expect this to be 0?您需要实现/使用 miller-rabin 算法
Handbook of Applied Cryptography
第 4.24 章
http://www.cacr.math.uwaterloo.ca/hac/about /chap4.pdf
You need to implement/use miller-rabin algorithm
Handbook of Applied Cryptography
chapter 4.24
http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf