简体   繁体   English

使用Java的BigInteger可能素数

[英]Working with Java's BigInteger probable primes

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点?

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. 您的数字从1开始,到9(小于10)停止,在每次迭代中都打印一个数字。 The above stops when the number is greater than the upper bound. 当数字大于上限时,以上操作停止。

您已将其设置为在(i <10)的位置运行,而不是在素数的值大于10时停止

You are counting i from lowerASInt to upperAsInt . 您正在将ilowerASIntupperAsInt You are counting i from 1 to 10. The statement i++ increments i with 1 (one). 您将i从1计数到10。语句i++ i加1(一)。

So your loop reads: while i is less than 10, print a prime and increment i with 1. 因此,您的循环显示为:当i小于10时,打印质数并以1递增i

So you will get the first 9 results. 这样您将获得前9个结果。

You are incrementing i by one each time, so it's going to run from i=1 till i=10 (9 times). 您每次将i递增1,因此它将从i = 1到i = 10(9次)运行。 if you want it to stop earlier set i = intermediate. 如果您希望它更早停止,请设置i =中间。

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 . 请不要对上述流使用parallel(),因为它会降低性能。 As a rule of thumb please don't parallelize stream if you have Stream.iterate() or Stream.limit() in your code . 根据经验,如果代码中包含Stream.iterate()或Stream.limit(),请不要并行化流。 A simple benchmark in my vm shows the parallel version is 4 times slower than the iterative one 我的虚拟机中的一个简单基准测试表明并行版本比迭代版本慢4倍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM