Advertisement
mzenko

PrimeSeive--Corrected

Jan 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. //Matt Zenko
  2. //APCS
  3. //1/7/2017
  4. package com.pepperoni.main;
  5. import java.text.DecimalFormat;
  6. import java.util.Scanner;
  7. public class PrimeSieve {
  8.     public static int totalPrimes = 0, MAX, runningPrime = 0, currIndex = 0;//global variable
  9.     public static void computePrimes(boolean[] primes){//compute primes method
  10.         for(int i = 0; i < primes.length; i++)//initialize primes to true, with 0 and 1 automatically being false
  11.             primes[i] = true;
  12.         primes[0] = false; primes[1] = false;
  13.         System.out.println("Computing...\n");
  14.         for(int i = 2; i < primes.length; i ++)//loops through all of the numbers in primes
  15.             for(int j = 2 * i; j < primes.length; j+=i)//marks all of the multiples of the number starting at 2 * the number as false
  16.                 primes[j] = false;                      //Doing so prevents counting the number as a multiple of itself
  17. /*commented below are alternate methods for doing the calculation, although they are not the 'correct' algorithm
  18.        
  19.         for (int i = 2; i < primes.length; i++){
  20.             for(int j = i; j < primes.length; j++){
  21.                 currIndex = i * j;
  22.                 if(currIndex < primes.length)primes[currIndex] = false;
  23.                 else break;
  24.             }
  25.         }
  26.         for(int i = 2; i < primes.length; i++){//iterates through array
  27.             if(primes[i] == true)//if the number is a prime
  28.                 for(int j = i; j < primes.length; j++)//mark all multiples of that number that are not that number as not prime
  29.                     if((j % i == 0) && j != i) primes[j] = false;
  30.         }
  31. */
  32.     }
  33.     public static void displayPrimes(boolean[] primes){//display primes method
  34.         for(int i = 0; i < primes.length; i++)//find total number of primes
  35.             if(primes[i] == true) totalPrimes++;
  36.         int bigPrime = 0;
  37.         for(int i = primes.length - 1; i > 0; i--)//finds the largest prime number in the set by going through the boolean[] in reverse
  38.             if(primes[i] == true){
  39.                 bigPrime = i;
  40.                 break;
  41.             }
  42.         int bpl = (int)(Math.log10(bigPrime) + 1);//finds the number of digits that the largest prime has
  43.         StringBuilder formatter = new StringBuilder();
  44.         for(int i = 0; i < bpl; i++)//creates a string with the same number of zeroes that the largest prime has digits
  45.             formatter.append(0);
  46.         DecimalFormat df = new DecimalFormat((new String(formatter)));//new decimal format created based on the size of the largest prime
  47.         System.out.println("There are " + totalPrimes + " primes between 0 and " + (primes.length - 1) + " (inclusive).\n");//prints info
  48.         for(int i = 0; i < primes.length; i++){//if it is prime, format and then print it, followed by a space
  49.             if(primes[i] == true){
  50.                 System.out.print(df.format(i) + " ");
  51.                 runningPrime++;
  52.                 if(runningPrime % 25 == 0) System.out.println();//every 25 primes, go to a new line
  53.             }
  54.         }
  55.         System.out.println("\n\ndone with " + totalPrimes + " total primes");
  56.     }
  57.     public static void main(String[] args) {
  58.         System.out.println("What is the upper bound?");//required main method
  59.         Scanner kbReader = new Scanner(System.in);
  60.         MAX = kbReader.nextInt();
  61.         boolean[] primes = new boolean[MAX + 1];
  62.         computePrimes(primes);
  63.         displayPrimes(primes);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement