Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class lego {
  5.  
  6.     static List<Integer> ns = new ArrayList<>();
  7.     static Writer writer = null;
  8.     static Scanner in;
  9.     static long lst;
  10.     static long start;
  11.     static int cpl;
  12.  
  13.  
  14.     static Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  15.  
  16.  
  17.     public static boolean alive = true;
  18.  
  19.     public static void main(String[] args) {
  20.  
  21.         try {
  22.             in = new Scanner(System.in);
  23.             writer = new BufferedWriter(new FileWriter(new File("primes.txt")));
  24.         } catch (Exception e) {
  25.             e.printStackTrace();
  26.             System.exit(1);
  27.         }
  28.  
  29.  
  30.  
  31.         Thread controll = new Thread(new Runnable() {
  32.             @Override
  33.             public void run() {
  34.                 while (true) {
  35.                     System.out.print("Enter \"stop\" to stop the loop");
  36.                     String c = in.nextLine();
  37.                     if (c.equalsIgnoreCase("stop")) {
  38.                         alive = false;
  39.                     }
  40.                 }
  41.             }
  42.         });
  43.  
  44.         int i = 1;
  45.         controll.start();
  46.         lst = java.time.Instant.now().getEpochSecond();
  47.         start = java.time.Instant.now().getEpochSecond() - 1;
  48.         while (alive) {
  49.             boolean isPrime = true;
  50.             for (int x=2; x<i; x++) {
  51.                 int w = i%x;
  52.                 //System.out.println(i + "%" + x + "=" + w);
  53.                 if (w==0) {
  54.                     isPrime = false;
  55.                 }
  56.             }
  57.  
  58.             if (isPrime) {
  59.                 try {
  60.                     writer.write("" + i + "\n");
  61.                 } catch (Exception e) {
  62.                     e.printStackTrace();
  63.                 }
  64.             }
  65.             i++;
  66.             cpl++;
  67.  
  68.             if (i%100 == 0) {
  69.                 long seconds = java.time.Instant.now().getEpochSecond();
  70.                 System.out.println("\n " + i + " calculations completed!");
  71.                 long cps = cpl / (seconds - start);
  72.                 lst = seconds;
  73.                 //cpl = 0;
  74.                 System.out.println("Speed: " + cps + " calculations per second");
  75.             }
  76.         }
  77.  
  78.         //writer.write((i+3) + ": " + x + "\n");
  79.         try {writer.close();} catch (Exception ex) {/*ignore*/}
  80.         controll.stop();
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement