使用Java的BigInteger可能的素数

时间:2022-07-19 16:47:29

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:

当它以1 10运行时,输出为:

2
3
5
7
11
13
17
19
23

Why doesn't it stop at 7?

为什么不在7点停止?

5 个解决方案

#1


Because your program says run times (1 to 9) not stop below 10. Instead of your loop you probably want:

因为你的程序说运行时间(1到9)不会停在10以下而不是你的循环,你可能想要:

BigIntegerupper = BigInteger.valueOf(upperAsInt);
while (intermediate.compareTo(upper) <= 0) {
  System.out.println(intermediate);
  intermediate = intermediate.nextProbablePrime();
}

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.

看到不同?您从1开始并在9(小于10)处停止,在每次迭代时打印一个数字。当数字大于上限时,上面停止。

#2


You have it set to run where (i<10), not to stop when the value of a prime is greater than 10

你将它设置为运行在哪里(i <10),而不是在素数值大于10时停止

#3


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.

你每次递增1,所以它将从i = 1直到i = 10(9次)。如果你想要它先停止设置i =中间。

#4


You are counting i from lowerASInt to upperAsInt. You are counting i from 1 to 10. The statement i++ increments i with 1 (one).

你正在计算我从lowerASInt到upperAsInt。你从1到10计算i。语句i ++以1(一)递增i。

So your loop reads: while i is less than 10, print a prime and increment i with 1.

所以你的循环读取:当我小于10时,打印素数并用1递增i。

So you will get the first 9 results.

所以你将获得前9个结果。

#5


This works if you use JDK8

这适用于使用JDK8的情况

 BigInteger lower=BigInteger.valueOf(1);
        BigInteger high=BigInteger.valueOf(100);
        Stream.iterate(lower, BigInteger::nextProbablePrime).limit(high.longValueExact())
                .filter(p -> p.compareTo(high) <= 0).forEach(System.out::println);

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

请不要对上面的流使用parallel(),因为它会降低性能。根据经验,如果您的代码中包含Stream.iterate()或Stream.limit(),请不要并行化流。我的vm中的一个简单基准测试表明,并行版本比迭代版本慢4倍

#1


Because your program says run times (1 to 9) not stop below 10. Instead of your loop you probably want:

因为你的程序说运行时间(1到9)不会停在10以下而不是你的循环,你可能想要:

BigIntegerupper = BigInteger.valueOf(upperAsInt);
while (intermediate.compareTo(upper) <= 0) {
  System.out.println(intermediate);
  intermediate = intermediate.nextProbablePrime();
}

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.

看到不同?您从1开始并在9(小于10)处停止,在每次迭代时打印一个数字。当数字大于上限时,上面停止。

#2


You have it set to run where (i<10), not to stop when the value of a prime is greater than 10

你将它设置为运行在哪里(i <10),而不是在素数值大于10时停止

#3


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.

你每次递增1,所以它将从i = 1直到i = 10(9次)。如果你想要它先停止设置i =中间。

#4


You are counting i from lowerASInt to upperAsInt. You are counting i from 1 to 10. The statement i++ increments i with 1 (one).

你正在计算我从lowerASInt到upperAsInt。你从1到10计算i。语句i ++以1(一)递增i。

So your loop reads: while i is less than 10, print a prime and increment i with 1.

所以你的循环读取:当我小于10时,打印素数并用1递增i。

So you will get the first 9 results.

所以你将获得前9个结果。

#5


This works if you use JDK8

这适用于使用JDK8的情况

 BigInteger lower=BigInteger.valueOf(1);
        BigInteger high=BigInteger.valueOf(100);
        Stream.iterate(lower, BigInteger::nextProbablePrime).limit(high.longValueExact())
                .filter(p -> p.compareTo(high) <= 0).forEach(System.out::println);

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

请不要对上面的流使用parallel(),因为它会降低性能。根据经验,如果您的代码中包含Stream.iterate()或Stream.limit(),请不要并行化流。我的vm中的一个简单基准测试表明,并行版本比迭代版本慢4倍