0

设置打印出所有错误值,这些错误值是素数,但打印出 25 个。3, 5, 7, 8, 9, 11, 13, 14, 15, 17, 19, 20, 21, 23, 24,不知道为什么其中一些会溜走。对此事的任何见解都会很好。

或者只是将我指向写入方向。为什么要打印 8 等非质数?

import java.util.Arrays;
import java.util.Scanner;
class Sieve {
      public static void main(String args[]) {
              Scanner inputScanner;
              inputScanner = new Scanner(System.in);
              //determine max value
              System.out.println("I will determine all the primality of a set of numbers, enter the max");
              int n = Integer.parseInt (inputScanner.nextLine());
              boolean[] truedBooleanArray = calcBooleanMax (n);
              //call upon function to check primality
              boolean [] primeNumbers = calcPrimality (truedBooleanArray);
              // call upon function to print out prime numbers
              printPrimes(primeNumbers);
      }

      public static boolean[] calcBooleanMax(int maxNumber) {
              boolean [] maxNumberArray = new boolean [maxNumber];
              maxNumberArray[0] = false;
              maxNumberArray[1] = false;
              //asigns  1, 0 to false
              //change all boleans within array from false to true!
              for(int i=1; i < maxNumber; i++) {
                      maxNumberArray [i] = true;
              }
              return maxNumberArray;
      }

      public static boolean[] calcPrimality(boolean [] truedBooleans) {
              for(int i = 2; i <=truedBooleans.length; i++) {
                      //check every number greater than 1 for primality.
                      if (truedBooleans[i-1]) {

                      }
                      //finds multiples and makes sure they arent stored
                      for(int j = 2*i; j <= truedBooleans.length; j+= i) {
                              truedBooleans[j-1] = false;
                      }
              } 
              return truedBooleans;
      }

      public static void printPrimes(boolean [] thePrimeNumbers){
              System.out.println("The prime numbers are [");
              for(int i = 2; i<thePrimeNumbers.length; i++) {
                      if(thePrimeNumbers[i] == false ) {
                              System.out.print(i + ", ");
                      }
              }
      }
}
4

2 回答 2

0

一个更简单的解决方案是对算法的字面解释较少。您可以保留当前素数的列表,而不是保留布尔值的文字列表。这使代码更简单,更易于阅读。

这是一个解决方案的示例(依赖于 Java 8 流):

class Sieve {
    private long current = 2;
    private final List<Long> primes = new ArrayList<>();

    public long nextPrime() {
        while (primes.stream().anyMatch(p -> current % p == 0))
            current++;
        primes.add(current);
        return current;
    }
}
于 2015-10-26T03:28:16.270 回答
0

你有几个错误。

  • 数组必须比给定的最大值大一
  • 您在初始化时不小心将一个添加回筛子
  • 从筛子中取出倍数时,必须首先确保初始数字“i”仍在筛子中
  • 您要打印仍在筛子中的项目,因此请在 true 而不是 false 时打印

这是固定代码

public static boolean[] calcBooleanMax(int maxNumber) {
    boolean [] maxNumberArray = new boolean [maxNumber+1];
    maxNumberArray[0] = false;
    maxNumberArray[1] = false; 
    //asigns  1, 0 to false
    //change all boleans within array from false to true!
    for(int i=2;i < maxNumber+1; i++) {
        maxNumberArray [i] = true;

    }
    return maxNumberArray;
}

public static boolean[] calcPrimality(boolean [] truedBooleans){
    for(int i = 2; i <truedBooleans.length; i++) {
        if(truedBooleans[i]) {
            //finds multiples and makes sure they arent stored
            for(int j = 2*i; j < truedBooleans.length; j+= i) {
                truedBooleans[j] = false;
            }
        }
    }
    return truedBooleans;
}


public static void printPrimes(boolean [] thePrimeNumbers){
    System.out.println("The prime numbers are [");
    for(int i = 2;i<thePrimeNumbers.length;i++) {
        if(thePrimeNumbers[i] ) {
            System.out.print(i + ", "); 
        }
    }
}
于 2015-10-26T03:08:43.827 回答