首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >关于erastothenes素性问题的筛子

关于erastothenes素性问题的筛子
EN

Stack Overflow用户
提问于 2015-10-26 10:42:36
回答 2查看 44关注 0票数 0

设置为打印出所有的假值,这些值是质数,但它打印的是25个值中的所有值。3,5,7,8,9,11,13,14,15,17,19,20,21,23,24,不知道为什么他们中的一些人错过了。任何对这件事的洞察都会很好。

或者简单地将我指向写入方向。为什么要打印像8这样的非质数?

代码语言:javascript
复制
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 + ", ");
                      }
              }
      }
}
EN

回答 2

Stack Overflow用户

发布于 2015-10-26 11:08:44

您有一些错误。

当initializing

  • When从筛子中删除倍数时,您必须首先确保初始数字"i“仍然在筛子中您想要打印仍在筛子中的项,因此当打印时为true而不是false

这是固定的代码

代码语言:javascript
复制
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 + ", "); 
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2015-10-26 11:28:16

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

下面是一个解决方案的示例(依赖于Java 8 streams):

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33337702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档