0

Set up to print out all false values which are prime numbers however out of 25 it prints. 3, 5, 7, 8, 9, 11, 13, 14, 15, 17, 19, 20, 21, 23, 24, not sure why some of them slip by. Any insight into the matter would be nice.

Or simply pointing me in the write direction. Why are the non-prime numbers such as 8 being printed?

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 + ", ");
                      }
              }
      }
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
asdf
  • 29
  • 4

2 Answers2

0

You have a few errors.

  • The array must be one larger than the given max
  • You are accidentally adding one back to the sieve when initializing
  • When removing multiples from the sieve, you must first make sure the initial number "i" is still in the sieve
  • You want to print the items that are still in the sieve, so print when true rather than false

Here is the fixed code

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 + ", "); 
        }
    }
}
SimCard
  • 305
  • 3
  • 13
0

A simpler solution is a less literal interpretation of the algorithm. Rather than keeping a literal list of booleans, you can keep a list of the current primes. This makes the code simpler and easier to read.

Here is an example of a solution (that relies on Java 8 streams):

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;
    }
}
sprinter
  • 27,148
  • 6
  • 47
  • 78