Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Matt Zenko
- //APCS
- //1/7/2017
- package com.pepperoni.main;
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class PrimeSieve {
- public static int totalPrimes = 0, MAX, runningPrime = 0, currIndex = 0;//global variable
- public static void computePrimes(boolean[] primes){//compute primes method
- for(int i = 0; i < primes.length; i++)//initialize primes to true, with 0 and 1 automatically being false
- primes[i] = true;
- primes[0] = false; primes[1] = false;
- System.out.println("Computing...\n");
- for(int i = 2; i < primes.length; i ++)//loops through all of the numbers in primes
- 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
- primes[j] = false; //Doing so prevents counting the number as a multiple of itself
- /*commented below are alternate methods for doing the calculation, although they are not the 'correct' algorithm
- for (int i = 2; i < primes.length; i++){
- for(int j = i; j < primes.length; j++){
- currIndex = i * j;
- if(currIndex < primes.length)primes[currIndex] = false;
- else break;
- }
- }
- for(int i = 2; i < primes.length; i++){//iterates through array
- if(primes[i] == true)//if the number is a prime
- for(int j = i; j < primes.length; j++)//mark all multiples of that number that are not that number as not prime
- if((j % i == 0) && j != i) primes[j] = false;
- }
- */
- }
- public static void displayPrimes(boolean[] primes){//display primes method
- for(int i = 0; i < primes.length; i++)//find total number of primes
- if(primes[i] == true) totalPrimes++;
- int bigPrime = 0;
- for(int i = primes.length - 1; i > 0; i--)//finds the largest prime number in the set by going through the boolean[] in reverse
- if(primes[i] == true){
- bigPrime = i;
- break;
- }
- int bpl = (int)(Math.log10(bigPrime) + 1);//finds the number of digits that the largest prime has
- StringBuilder formatter = new StringBuilder();
- for(int i = 0; i < bpl; i++)//creates a string with the same number of zeroes that the largest prime has digits
- formatter.append(0);
- DecimalFormat df = new DecimalFormat((new String(formatter)));//new decimal format created based on the size of the largest prime
- System.out.println("There are " + totalPrimes + " primes between 0 and " + (primes.length - 1) + " (inclusive).\n");//prints info
- for(int i = 0; i < primes.length; i++){//if it is prime, format and then print it, followed by a space
- if(primes[i] == true){
- System.out.print(df.format(i) + " ");
- runningPrime++;
- if(runningPrime % 25 == 0) System.out.println();//every 25 primes, go to a new line
- }
- }
- System.out.println("\n\ndone with " + totalPrimes + " total primes");
- }
- public static void main(String[] args) {
- System.out.println("What is the upper bound?");//required main method
- Scanner kbReader = new Scanner(System.in);
- MAX = kbReader.nextInt();
- boolean[] primes = new boolean[MAX + 1];
- computePrimes(primes);
- displayPrimes(primes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement