Advertisement
Guest User

SieveGraphicalSimulation

a guest
Jan 28th, 2015
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.46 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.awt.*;
  4.  
  5. public class SieveGraphicalSimulation {
  6.  
  7.     public static final Color BLACK      = Color.BLACK;
  8.     public static final Color BLUE       = Color.BLUE;
  9.     public static final Color CYAN       = Color.CYAN;
  10.     public static final Color DARK_GRAY  = Color.DARK_GRAY;
  11.     public static final Color GRAY       = Color.GRAY;
  12.     public static final Color GREEN      = Color.GREEN;
  13.     public static final Color LIGHT_GRAY = Color.LIGHT_GRAY;
  14.     public static final Color MAGENTA    = Color.MAGENTA;
  15.     public static final Color ORANGE     = Color.ORANGE;
  16.     public static final Color PINK       = Color.PINK;
  17.     public static final Color RED        = Color.RED;
  18.     public static final Color WHITE      = Color.WHITE;
  19.     public static final Color YELLOW     = Color.YELLOW;
  20.     public static final Color[] colours = {CYAN,MAGENTA,ORANGE,PINK,RED,YELLOW};
  21.  
  22.     public static void main(String[] args) {
  23.         try{
  24.             Scanner scanner = new Scanner(System.in);
  25.             System.out.println( "Sieve of Eratosthenes (Prime Finder)" + "\nPlease enter the maximum number you wish to see up until?" );
  26.             int input = scanner.nextInt();
  27.             int counter = 0;
  28.             while(counter==0){
  29.                 if (input<=1){
  30.                     System.err.println("Error, please enter positive whole numbers only please (from 2 onwards).");
  31.                     input = scanner.nextInt();
  32.                 }
  33.                 else{
  34.                     counter=1;
  35.                 }
  36.             }
  37.             int[] numbers = numbers(input);
  38.             StdDraw.setCanvasSize(800, 750);
  39.             double scale =(double)input/1000;
  40.             StdDraw.setXscale(0.0,1.3);
  41.             StdDraw.setYscale(0.0,1.0);
  42.             boolean[] multiples = markOfMultiples(numbers,input);
  43.             int newSize = input-1;
  44.             int[] output = printSquares(multiples,numbers,input,newSize);
  45.             StdDraw.show(1500);
  46.             int[] newArray = colourMultiples(multiples,input,numbers,newSize);
  47.             scanner.close();
  48.         }
  49.         catch(java.util.InputMismatchException exception)
  50.         {
  51.             System.err.println("Error, please enter whole numbers only."
  52.                     + "\nPlease restart the program and try again.");
  53.         }
  54.         catch(NullPointerException exception2)
  55.         {
  56.             System.err.println("Error, please enter whole numbers only."
  57.                     + "\nPlease restart the program and try again.");
  58.         }
  59.     }
  60.     public static int[] numbers (int input){ //creates the initial array of numbers, from 2 until the user input
  61.         int[] multiples = new int[input];
  62.         int n = 2;
  63.         int size = input-1;
  64.         for (int i=0;i<size;i++){
  65.             multiples[i] = n;
  66.             n++;
  67.         }  
  68.         return multiples;
  69.     }
  70.     public static int[] printSquares (boolean[] multiples, int[]numbers, int input, int newSize){
  71.         int squareMaker = (int)Math.sqrt(input);
  72.         double x = 0.15;
  73.         double y = 0.95;
  74.         double r = 0.05;
  75.         int counter = 0;
  76.         for(int i=0;i<newSize;i++){
  77.             int number = numbers[i];
  78.             if (i==squareMaker-1 || counter == squareMaker){
  79.                 counter = 0;
  80.                 x = 0.045;
  81.                 y=(y-0.105);
  82.             }
  83.             double Scale = input/1000;
  84.             StdDraw.setXscale(0.0,1.3);
  85.             StdDraw.setYscale(0.0,1.0);
  86.             String numberOutput = Integer.toString(number);
  87.             StdDraw.setPenColor(LIGHT_GRAY);
  88.             StdDraw.filledSquare(x, y, r);
  89.             StdDraw.setPenColor(BLACK);
  90.             Font textFont = new Font("SansSerif", Font.PLAIN, 10);
  91.             StdDraw.setFont(textFont);
  92.             StdDraw.text(x,y,numberOutput);
  93.             counter++;
  94.             x=x+(.105);
  95.         }
  96.         return null;
  97.     }
  98.  
  99.     public static boolean[] markOfMultiples (int[] numbers, int input) { //creates a boolean array that marks multiples of a base prime with 'false'
  100.         boolean[] primeFinder = new boolean[input];
  101.         Arrays.fill(primeFinder, Boolean.TRUE);
  102.         int size = input - 1;  
  103.         for(int i=0;i<size;i++){
  104.             int number = numbers[i];
  105.             for(int p=1;p<size;p++){
  106.                 int divisor = numbers[p];
  107.                 if (divisor!=number){
  108.                     if (divisor%number==0){
  109.                         primeFinder[p] = false;     //sets the position (matching the numbers array) in the boolean array to false
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.         return primeFinder;
  115.     }
  116.     public static int[] colourMultiples (boolean[] multiples, int input, int[] numbers, int newSize){  
  117.         int squareMaker = (int)Math.sqrt(input);
  118.         int[] primeNumbers = new int[newSize];
  119.         double xIncrease = 0.15;
  120.         double yIncrease = 0.95;
  121.         int ovCounter = 0;
  122.         Font primeOutput = new Font("SansSerif", Font.BOLD, 20);
  123.         Font textFont = new Font("SansSerif", Font.PLAIN, 15);
  124.         StdDraw.setPenColor(BLACK);
  125.         StdDraw.setFont(primeOutput);
  126.         double fontX = (squareMaker*0.105);
  127.         double fontY = 0.95;
  128.         StdDraw.text(fontX+0.15,fontY,"Prime Numbers:");
  129.         int t=100;
  130.         for (int i = 0; i< newSize;i++){
  131.             double x = 0.15;
  132.             double y = 0.95;
  133.             double r = 0.05;                                                               
  134.             int counter = 0;
  135.             if (i==squareMaker-1 || ovCounter == squareMaker){
  136.                 ovCounter = 0;
  137.                 xIncrease = 0.045;
  138.                 yIncrease=(yIncrease-0.105);
  139.                 fontX = (squareMaker*0.105);
  140.                 fontY -= 0.05;
  141.             }
  142.             int number = numbers[i];
  143.             if(multiples[i]==true){
  144.                 StdDraw.setPenColor(GREEN);
  145.                 StdDraw.filledSquare(xIncrease, yIncrease, r);
  146.                 String numberOutput = Integer.toString(number);
  147.                 StdDraw.setFont(textFont);
  148.                 StdDraw.setPenColor(BLACK);
  149.                 StdDraw.text(fontX+0.05,(i>=0||i<=squareMaker)?(fontY-0.05):fontY,numberOutput+" ");
  150.                 StdDraw.text(xIncrease,yIncrease,numberOutput);
  151.                 for (int p = 0; p< newSize;p++){
  152.                     StdDraw.show(t);
  153.                     int divisor = numbers[p];
  154.                     if (p==squareMaker-1 || counter == squareMaker){
  155.                         counter = 0;
  156.                         x = 0.045;
  157.                         y=(y-0.105);
  158.                     }
  159.                     if (divisor!=number){
  160.                         if (divisor%number==0){
  161.                             StdDraw.setPenColor(RED);
  162.                             StdDraw.filledSquare(x, y, r);
  163.                             String divisorOutput = Integer.toString(divisor);
  164.                             StdDraw.setPenColor(BLACK);
  165.                             StdDraw.setFont(textFont);
  166.                             StdDraw.text(x,y,divisorOutput);
  167.                         }
  168.                     }
  169.                     counter++;
  170.                     x=x+(.105);
  171.                 }
  172.                 if(t>10){
  173.                     t=t-10;
  174.                 }
  175.                 x = 0.15;
  176.                 y = 0.95;
  177.                 r = 0.05;
  178.                 counter=0;
  179.                 StdDraw.show(500);
  180.                 for (int p = 0; p< newSize;p++){
  181.                     int divisor = numbers[p];
  182.                     if (p==squareMaker-1 || counter == squareMaker){
  183.                         counter = 0;
  184.                         x = 0.045;
  185.                         y=(y-0.105);
  186.                     }
  187.                     if (divisor!=number){
  188.                         if (divisor%number==0){
  189.                             StdDraw.setPenColor(PINK);
  190.                             StdDraw.filledSquare(x, y, r);
  191.                             String divisorOutput = Integer.toString(divisor);
  192.                             StdDraw.setPenColor(BLACK);
  193.                             StdDraw.setFont(textFont);
  194.                             StdDraw.text(x,y,divisorOutput);
  195.                         }
  196.                     }
  197.                     counter++;
  198.                     x=x+(.105);
  199.                 }
  200.                 fontX += 0.05;
  201.             }
  202.             StdDraw.show(5*t);
  203.             ovCounter++;
  204.             xIncrease=xIncrease+(.105);
  205.         }
  206.         return primeNumbers;
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement