Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class lego {
- static List<Integer> ns = new ArrayList<>();
- static Writer writer = null;
- static Scanner in;
- static long lst;
- static long start;
- static int cpl;
- static Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- public static boolean alive = true;
- public static void main(String[] args) {
- try {
- in = new Scanner(System.in);
- writer = new BufferedWriter(new FileWriter(new File("primes.txt")));
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(1);
- }
- Thread controll = new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- System.out.print("Enter \"stop\" to stop the loop");
- String c = in.nextLine();
- if (c.equalsIgnoreCase("stop")) {
- alive = false;
- }
- }
- }
- });
- int i = 1;
- controll.start();
- lst = java.time.Instant.now().getEpochSecond();
- start = java.time.Instant.now().getEpochSecond() - 1;
- while (alive) {
- boolean isPrime = true;
- for (int x=2; x<i; x++) {
- int w = i%x;
- //System.out.println(i + "%" + x + "=" + w);
- if (w==0) {
- isPrime = false;
- }
- }
- if (isPrime) {
- try {
- writer.write("" + i + "\n");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- i++;
- cpl++;
- if (i%100 == 0) {
- long seconds = java.time.Instant.now().getEpochSecond();
- System.out.println("\n " + i + " calculations completed!");
- long cps = cpl / (seconds - start);
- lst = seconds;
- //cpl = 0;
- System.out.println("Speed: " + cps + " calculations per second");
- }
- }
- //writer.write((i+3) + ": " + x + "\n");
- try {writer.close();} catch (Exception ex) {/*ignore*/}
- controll.stop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement