筛选出一个时代的问题

时间:2015-10-26 02:42:36

标签: java sieve

设置打印出所有假值,这些值是素数,但是打印出来的是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 + ", ");
                      }
              }
      }
}

2 个答案:

答案 0 :(得分:0)

你有一些错误。

  • 数组必须大于给定的最大值
  • 初始化时,您不小心将一个添加回筛子
  • 从筛子中取出多个时,必须首先确保初始数字“i”仍然在筛子中
  • 您想要打印仍在筛子中的物品,因此在真实时打印而不是错误

这是固定代码

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 + ", "); 
        }
    }
}

答案 1 :(得分: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;
    }
}