import java.io.*; import java.lang.*; public class Prime { private BufferedReader in; //Input reader private double time1; //Start Time private double time2; //End Time private double totaltime; //Difference between start-end times private String st; //String to read input private int limit; //Count limit private int x; private int flag; private int counter; //Constructor public Prime () throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); x = 2; counter = 1; System.out.print("Count primes up-to: "); st = in.readLine(); try {limit = Integer.parseInt(st); } catch (NumberFormatException e) {} time1 = System.currentTimeMillis(); System.out.println("Start-Time: "+time1); while (x < limit) { flag = Prime(x); if (flag > 0){ System.out.print(x+", "); counter++; x++; } else x++; } time2 = System.currentTimeMillis(); System.out.println(""); System.out.println("End-Time: "+time2); totaltime = time2 - time1; System.out.println("Number of primes between 1 and "+limit+": "+counter); System.out.println("Java Run-Time: "+totaltime); } //This method returns 1 if the given x is a prime //Returns -1 if the given x is not a prime private int Prime (int x) { int result = 1; int i=2; while (i <= x/2) { if (x%i==0){ result = -1; break; } else i++; } if (x%i==0) result = -1; return result; } public static void main (String args[]) throws IOException {new Prime();} }