使用 Java 的 BigInteger 可能素数

发布于 2024-07-26 06:59:51 字数 919 浏览 3 评论 0原文

我想打印两个数字之间的所有素数。 这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紅太極 2024-08-02 06:59:51

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

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

看到区别了吗? 你的从 1 开始,到 9 停止(小于 10),在每次迭代时打印一个数字。 当数字大于上限时,上述过程停止。

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

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.

怀念你的温柔 2024-08-02 06:59:51

您将其设置为在 (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 2024-08-02 06:59:51

每次将 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.

皓月长歌 2024-08-02 06:59:51

您正在从 lowerASIntupperAsInt 计数 i。 你正在从 1 数到 10。
语句 i++i 增加 1(一)。

所以你的循环如下:
i小于10时,打印一个质数并将i加1。

这样你就会得到前9个结果。

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

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

So you will get the first 9 results.

我一向站在原地 2024-08-02 06:59:51

如果您使用 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);

请不要对上述流使用并行(),因为它会降低性能。 根据经验,如果代码中有 Stream.iterate() 或 Stream.limit() ,请不要并行化流。 我的虚拟机中的一个简单基准测试显示并行版本比迭代版本慢 4 倍

This works if you use 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文