简体   繁体   English

使用 Java 中的列表方法找到边界之间的素数

[英]find the prime numbers between bounds using list method in Java

public class PrimeNumbers {
    public static void main(String[] args) {
        System.out.println(getPrimeNumbers(3, 10));
    }

    public static List<Integer> getPrimeNumbers(int lowerBound, int upperBound) {
        List<Integer> numbers = new ArrayList<>();
        for (int i = lowerBound; i <= upperBound; i++) {
            for (int j = 2; j < upperBound; j++) {
                if (i % j == 0) {
                    break;
                } else {
                    numbers.add(i);
                }
            }
        }
        return numbers;
    }
}

This is the code so far but the prime numbers keep repeating, I need help to stop the repetition这是到目前为止的代码,但素数不断重复,我需要帮助来停止重复

The problem is on this part:问题出在这部分:

for (int j = 2; j < upperBound; j++) {
    if (i % j == 0) {
        break;
    } else {
        numbers.add(i);
    }
}

You should only add the number as prime to the list if for all the numbers from 2 to upperBound i % j == 0 evaluates to false.如果从 2 到upperBound i % j == 0的所有数字评估为假,您应该只将数字作为素数添加到列表中。

Moreover, your inner-loop has another mistake:此外,您的内循环还有另一个错误:

for (int j = 2; j < upperBound; j++)

instead of upperBound you should use the variable i , namely:而不是upperBound你应该使用变量i ,即:

for (int j = 2; j < i; j++)

you can optimized to:您可以优化为:

for (int j = 2; j < i/2; j++)

or even better:`甚至更好:`

for (int j = 2; j <= (int) Math.sqrt(i); j++) 

So I would suggest you to extract a method with that logic as follows:因此,我建议您提取具有该逻辑的方法,如下所示:

private static boolean isPrime(int i) {
     final int upperLimit = (int) Math.sqrt(i);
     for (int j = 2; j <= upperLimit; j++)  {
        if (i % j == 0) {
            return false;
        }
    }
    return true;
}

and use it in your code as follows:并在您的代码中使用它,如下所示:

public static List<Integer> getPrimeNumbers(int lowerBound, int upperBound) {
    List<Integer> numbers = new ArrayList<>();
    for (int i = lowerBound; i <= upperBound; i++) {
        if(isPrime(i))
            numbers.add(i);
    }
    return numbers;
}

With Java Streams:使用 Java 流:

public static List<Integer> getPrimeNumbers(int lowerBound, int upperBound) {
    return IntStream.range(lowerBound, upperBound)
                    .filter(PrimeNumbers::isPrime)
                    .boxed()
                    .collect(Collectors.toList());
}

The isPrime method can be further optimized, for instance (assuming i positive): isPrime方法可以进一步优化,例如(假设i为正):

private static boolean isPrime(int i) {
     if (i < 2)  return false;
     else if (i == 2) return true;
      else if (i % 2 == 0) return false;

     final int upperLimit = (int) Math.sqrt(i);
     for (int j = 3; j <= upperLimit; j += 2)  {
        if (i % j == 0) {
            return false;
        }
    }
    return true;
}

If i is 0, 1, or a number divisible by 2 (excluding 2) then it is not a prime number.如果i是 0、1 或可被 2 整除的数(不包括 2),则它不是素数。 On the loop we just need to check the odd numbers, since we have checked before if n % 2 == 0 .在循环中,我们只需要检查奇数,因为我们之前已经检查过 if n % 2 == 0

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

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