使用 Java 的 BigInteger 可能素数
我想打印两个数字之间的所有素数。 这是我的代码:
package sphere;
import java.math.BigInteger;
import java.io.*;
class PrimeTest2 {
public static void main(String args[]) throws java.lang.Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String s = r.readLine();
String [] splitted = s.split(" ");
BigInteger lower = new BigInteger(splitted[0]);
BigInteger upper = new BigInteger(splitted[1]);
int lowerAsInt = Integer.parseInt(splitted[0]);
int upperAsInt = Integer.parseInt(splitted[1]);
BigInteger intermediate = lower;
for (int i=lowerAsInt; i<upperAsInt; i++) {
intermediate = intermediate.nextProbablePrime();
System.out.println(intermediate);
}
}
}
当它以 1 10 运行时,输出是:
2
3
5
7
11
13
17
19
23
为什么它不在 7 处停止?
I want to print all the prime numbers between two numbers. This is my code:
package sphere;
import java.math.BigInteger;
import java.io.*;
class PrimeTest2 {
public static void main(String args[]) throws java.lang.Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String s = r.readLine();
String [] splitted = s.split(" ");
BigInteger lower = new BigInteger(splitted[0]);
BigInteger upper = new BigInteger(splitted[1]);
int lowerAsInt = Integer.parseInt(splitted[0]);
int upperAsInt = Integer.parseInt(splitted[1]);
BigInteger intermediate = lower;
for (int i=lowerAsInt; i<upperAsInt; i++) {
intermediate = intermediate.nextProbablePrime();
System.out.println(intermediate);
}
}
}
When it's run with 1 10 the output is:
2
3
5
7
11
13
17
19
23
Why doesn't it stop at 7?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为你的程序说运行时间(1 到 9)不会在 10 以下停止。你可能想要的不是你的循环:
看到区别了吗? 你的从 1 开始,到 9 停止(小于 10),在每次迭代时打印一个数字。 当数字大于上限时,上述过程停止。
Because your program says run times (1 to 9) not stop below 10. Instead of your loop you probably want:
See the difference? Yours starts at 1 and stops at 9 (less than 10), printing a number on each iteration. The above stops when the number is greater than the upper bound.
您将其设置为在 (i<10) 处运行,而不是在素数的值大于 10 时停止
You have it set to run where (i<10), not to stop when the value of a prime is greater than 10
每次将 i 递增 1,因此它将从 i=1 运行到 i=10(9 次)。 如果你想让它早点停止,设置 i = 中间。
You are incrementing i by one each time, so it's going to run from i=1 till i=10 (9 times). if you want it to stop earlier set i = intermediate.
您正在从
lowerASInt
到upperAsInt
计数i
。 你正在从 1 数到 10。语句
i++
将i
增加 1(一)。所以你的循环如下:
当
i
小于10时,打印一个质数并将i
加1。这样你就会得到前9个结果。
You are counting
i
fromlowerASInt
toupperAsInt
. You are counting i from 1 to 10.The statement
i++
incrementsi
with 1 (one).So your loop reads:
while
i
is less than 10, print a prime and incrementi
with 1.So you will get the first 9 results.
如果您使用 JDK8,则此方法有效。
请不要对上述流使用并行(),因为它会降低性能。 根据经验,如果代码中有 Stream.iterate() 或 Stream.limit() ,请不要并行化流。 我的虚拟机中的一个简单基准测试显示并行版本比迭代版本慢 4 倍
This works if you use JDK8
Please don't use parallel() for the above stream , as it will slow down performance . As a rule of thumb please don't parallelize stream if you have Stream.iterate() or Stream.limit() in your code . A simple benchmark in my vm shows the parallel version is 4 times slower than the iterative one