Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- import java.awt.*;
- public class SieveGraphicalSimulation {
- public static final Color BLACK = Color.BLACK;
- public static final Color BLUE = Color.BLUE;
- public static final Color CYAN = Color.CYAN;
- public static final Color DARK_GRAY = Color.DARK_GRAY;
- public static final Color GRAY = Color.GRAY;
- public static final Color GREEN = Color.GREEN;
- public static final Color LIGHT_GRAY = Color.LIGHT_GRAY;
- public static final Color MAGENTA = Color.MAGENTA;
- public static final Color ORANGE = Color.ORANGE;
- public static final Color PINK = Color.PINK;
- public static final Color RED = Color.RED;
- public static final Color WHITE = Color.WHITE;
- public static final Color YELLOW = Color.YELLOW;
- public static final Color[] colours = {CYAN,MAGENTA,ORANGE,PINK,RED,YELLOW};
- public static void main(String[] args) {
- try{
- Scanner scanner = new Scanner(System.in);
- System.out.println( "Sieve of Eratosthenes (Prime Finder)" + "\nPlease enter the maximum number you wish to see up until?" );
- int input = scanner.nextInt();
- int counter = 0;
- while(counter==0){
- if (input<=1){
- System.err.println("Error, please enter positive whole numbers only please (from 2 onwards).");
- input = scanner.nextInt();
- }
- else{
- counter=1;
- }
- }
- int[] numbers = numbers(input);
- StdDraw.setCanvasSize(800, 750);
- double scale =(double)input/1000;
- StdDraw.setXscale(0.0,1.3);
- StdDraw.setYscale(0.0,1.0);
- boolean[] multiples = markOfMultiples(numbers,input);
- int newSize = input-1;
- int[] output = printSquares(multiples,numbers,input,newSize);
- StdDraw.show(1500);
- int[] newArray = colourMultiples(multiples,input,numbers,newSize);
- scanner.close();
- }
- catch(java.util.InputMismatchException exception)
- {
- System.err.println("Error, please enter whole numbers only."
- + "\nPlease restart the program and try again.");
- }
- catch(NullPointerException exception2)
- {
- System.err.println("Error, please enter whole numbers only."
- + "\nPlease restart the program and try again.");
- }
- }
- public static int[] numbers (int input){ //creates the initial array of numbers, from 2 until the user input
- int[] multiples = new int[input];
- int n = 2;
- int size = input-1;
- for (int i=0;i<size;i++){
- multiples[i] = n;
- n++;
- }
- return multiples;
- }
- public static int[] printSquares (boolean[] multiples, int[]numbers, int input, int newSize){
- int squareMaker = (int)Math.sqrt(input);
- double x = 0.15;
- double y = 0.95;
- double r = 0.05;
- int counter = 0;
- for(int i=0;i<newSize;i++){
- int number = numbers[i];
- if (i==squareMaker-1 || counter == squareMaker){
- counter = 0;
- x = 0.045;
- y=(y-0.105);
- }
- double Scale = input/1000;
- StdDraw.setXscale(0.0,1.3);
- StdDraw.setYscale(0.0,1.0);
- String numberOutput = Integer.toString(number);
- StdDraw.setPenColor(LIGHT_GRAY);
- StdDraw.filledSquare(x, y, r);
- StdDraw.setPenColor(BLACK);
- Font textFont = new Font("SansSerif", Font.PLAIN, 10);
- StdDraw.setFont(textFont);
- StdDraw.text(x,y,numberOutput);
- counter++;
- x=x+(.105);
- }
- return null;
- }
- public static boolean[] markOfMultiples (int[] numbers, int input) { //creates a boolean array that marks multiples of a base prime with 'false'
- boolean[] primeFinder = new boolean[input];
- Arrays.fill(primeFinder, Boolean.TRUE);
- int size = input - 1;
- for(int i=0;i<size;i++){
- int number = numbers[i];
- for(int p=1;p<size;p++){
- int divisor = numbers[p];
- if (divisor!=number){
- if (divisor%number==0){
- primeFinder[p] = false; //sets the position (matching the numbers array) in the boolean array to false
- }
- }
- }
- }
- return primeFinder;
- }
- public static int[] colourMultiples (boolean[] multiples, int input, int[] numbers, int newSize){
- int squareMaker = (int)Math.sqrt(input);
- int[] primeNumbers = new int[newSize];
- double xIncrease = 0.15;
- double yIncrease = 0.95;
- int ovCounter = 0;
- Font primeOutput = new Font("SansSerif", Font.BOLD, 20);
- Font textFont = new Font("SansSerif", Font.PLAIN, 15);
- StdDraw.setPenColor(BLACK);
- StdDraw.setFont(primeOutput);
- double fontX = (squareMaker*0.105);
- double fontY = 0.95;
- StdDraw.text(fontX+0.15,fontY,"Prime Numbers:");
- int t=100;
- for (int i = 0; i< newSize;i++){
- double x = 0.15;
- double y = 0.95;
- double r = 0.05;
- int counter = 0;
- if (i==squareMaker-1 || ovCounter == squareMaker){
- ovCounter = 0;
- xIncrease = 0.045;
- yIncrease=(yIncrease-0.105);
- fontX = (squareMaker*0.105);
- fontY -= 0.05;
- }
- int number = numbers[i];
- if(multiples[i]==true){
- StdDraw.setPenColor(GREEN);
- StdDraw.filledSquare(xIncrease, yIncrease, r);
- String numberOutput = Integer.toString(number);
- StdDraw.setFont(textFont);
- StdDraw.setPenColor(BLACK);
- StdDraw.text(fontX+0.05,(i>=0||i<=squareMaker)?(fontY-0.05):fontY,numberOutput+" ");
- StdDraw.text(xIncrease,yIncrease,numberOutput);
- for (int p = 0; p< newSize;p++){
- StdDraw.show(t);
- int divisor = numbers[p];
- if (p==squareMaker-1 || counter == squareMaker){
- counter = 0;
- x = 0.045;
- y=(y-0.105);
- }
- if (divisor!=number){
- if (divisor%number==0){
- StdDraw.setPenColor(RED);
- StdDraw.filledSquare(x, y, r);
- String divisorOutput = Integer.toString(divisor);
- StdDraw.setPenColor(BLACK);
- StdDraw.setFont(textFont);
- StdDraw.text(x,y,divisorOutput);
- }
- }
- counter++;
- x=x+(.105);
- }
- if(t>10){
- t=t-10;
- }
- x = 0.15;
- y = 0.95;
- r = 0.05;
- counter=0;
- StdDraw.show(500);
- for (int p = 0; p< newSize;p++){
- int divisor = numbers[p];
- if (p==squareMaker-1 || counter == squareMaker){
- counter = 0;
- x = 0.045;
- y=(y-0.105);
- }
- if (divisor!=number){
- if (divisor%number==0){
- StdDraw.setPenColor(PINK);
- StdDraw.filledSquare(x, y, r);
- String divisorOutput = Integer.toString(divisor);
- StdDraw.setPenColor(BLACK);
- StdDraw.setFont(textFont);
- StdDraw.text(x,y,divisorOutput);
- }
- }
- counter++;
- x=x+(.105);
- }
- fontX += 0.05;
- }
- StdDraw.show(5*t);
- ovCounter++;
- xIncrease=xIncrease+(.105);
- }
- return primeNumbers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement