Advertisement
Guest User

Untitled

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