December SPECIAL! For a limited time only. Get 20% discount on a LIFETIME PRO account!Want more features on Pastebin? Sign Up, it's FREE!
tweet
Guest

primes all ints

By: a guest on Nov 11th, 2015  |  syntax: Java  |  size: 1.31 KB  |  views: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print  |  QR code  |  clone
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package primeFinder;
  2.  
  3. import  java.util.Scanner;
  4.  
  5. public class PrimeFinder2 {
  6.        
  7.         public static void main(String[] args){
  8.                 double timeS, timeE;
  9.                 double timeT;
  10.                 Scanner input=new Scanner(System.in);
  11.                 for(;;){
  12.                         System.out.println("enter a positive integer greater than 1 to find all the primes less than it enter \"0\" to qutit");
  13.                        
  14.                                 long n=input.nextLong();
  15.                                 if (n<0||n==1){
  16.                                         System.out.println("enter a number greater than 1");
  17.                                 }
  18.                                 else if (n==0){
  19.                                         System.exit(0);
  20.                                 }
  21.                                 else{
  22.                                         System.out.println("valid number entered primes up to "+n+" are:");
  23.                                         timeS=System.currentTimeMillis();
  24.                                         primesToN(n);
  25.                                         timeE=System.currentTimeMillis();
  26.                                         timeT=(timeE-timeS)/1000;
  27.                                         System.out.println("it took "+timeT+" S to calculate primes up to "+n);
  28.                                 }
  29.                 }      
  30.         }//end of main
  31.         public static void primesToN(Long n){
  32.                 System.out.print("2, ");
  33.                 boolean print=false;
  34.                 int count=0;
  35.                 for(long i=3;i<=n;i++){
  36.                        
  37.                         for(int x=2;x<i;x++){
  38.                                 if(i%x==0){
  39.                                         print=false;
  40.                                         break;
  41.                                 }
  42.                                 else{
  43.                                         print=true;
  44.                                 }
  45.                         }
  46.                         if(print&&count!=4&&i!=n){
  47.                                 System.out.print(i+", ");
  48.                                 count++;
  49.                         }
  50.                         else if(print&&i!=n){
  51.                                 System.out.println(i+", ");
  52.                                 count=0;
  53.                         }
  54.                        
  55.                         print=false;
  56.                 }
  57.         }//end of primesToN
  58. }//end of prime finder
clone this paste RAW Paste Data
Top