daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Jun 2nd, 2017 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Licenced under the GNU General Public Licence v2 or later.
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintStream;
  8. public class Primes {
  9.     public static void main(String[] args) throws NumberFormatException, IOException{
  10.         System.out.println("gardenGnostic's Simple Prime Calculator");
  11.         System.out.println("Licenced under the GNU General Public Licence v2 or later.");
  12.         System.out.println("");
  13.        
  14.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  15.         System.out.print("Max number: ");
  16.         int maxNumber = Integer.parseInt(in.readLine());
  17.         System.out.print("Output location: ");
  18.         FileOutputStream file_out = new FileOutputStream(in.readLine());
  19.         PrintStream out = new PrintStream(file_out);
  20.        
  21.         out.println("Cacluated with gardenGnostic's Simple Prime Calculator\n");
  22.         out.println("Calculating all primes from 1 to "+maxNumber+".\n");
  23.        
  24.         long time = System.currentTimeMillis();
  25.        
  26.         boolean[] notprime = new boolean[maxNumber];
  27.         int primenumber = 1;
  28.         for(int i=2;i<=maxNumber;i++){
  29.             if(!notprime[i-1]){
  30.                 out.println("Prime #"+(primenumber++)+": "+i);
  31.                 for(int i2=i*2;i2<=maxNumber;i2+=i){
  32.                     notprime[i2-1] = true;
  33.                 }
  34.             }
  35.         }
  36.        
  37.         String t = "\nCacluated in "+((System.currentTimeMillis()-time)/1000)+" seconds.";
  38.         System.out.println(t);
  39.         out.print(t+"\nbluh bluh");
  40.        
  41.         file_out.flush();
  42.     }
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top