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 array list

By: a guest on Nov 11th, 2015  |  syntax: Java  |  size: 1.52 KB  |  views: 4  |  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. import  java.util.ArrayList;
  5.  
  6. public class PrimeFinder {
  7.        
  8.         public static void main(String[] args){
  9.                 double timeS, timeE;
  10.                 double timeT;
  11.                 Scanner input=new Scanner(System.in);
  12.                 for(;;){
  13.                         System.out.println("enter a positive integer greater than 1 to find all the primes less than it enter \"0\" to qutit");
  14.                        
  15.                                 long n=input.nextLong();
  16.                                 if (n<0||n==1){
  17.                                         System.out.println("enter a number greater than 1");
  18.                                 }
  19.                                 else if (n==0){
  20.                                         System.exit(0);
  21.                                 }
  22.                                 else{
  23.                                         System.out.println("valid number entered primes up to "+n+" are:");
  24.                                         timeS=System.currentTimeMillis();
  25.                                         primesToN(n);
  26.                                         timeE=System.currentTimeMillis();
  27.                                         timeT=(timeE-timeS)/1000;
  28.                                         System.out.println("it took "+timeT+" S to calculate primes up to "+n);
  29.                                 }
  30.                 }      
  31.         }//end of main
  32.         public static void primesToN(Long n){
  33.                 ArrayList<Long> primes=new ArrayList<Long>();
  34.                 primes.add((long) 2);
  35.                 long last=2;
  36.                 System.out.print("2, ");
  37.                 boolean print=false;
  38.                 int count=0;
  39.                 for(long i=3;i<=n;i++){
  40.                        
  41.                         for(int x=0;x<=primes.indexOf(last);x++){
  42.                                 if(i%primes.get(x)==0){
  43.                                         print=false;
  44.                                         break;
  45.                                 }
  46.                                 else{
  47.                                         print=true;
  48.                                 }
  49.                         }
  50.                         if(print&&count!=4&&i!=n){
  51.                                 primes.add(i);
  52.                                 last=i;
  53.                                 System.out.print(i+", ");
  54.                                 count++;
  55.                         }
  56.                         else if(print&&i!=n){
  57.                                 primes.add(i);
  58.                                 last=i;
  59.                                 System.out.println(i+", ");
  60.                                 count=0;
  61.                         }
  62.                        
  63.                         print=false;
  64.                 }
  65.         }//end of primesToN
  66. }//end of prime finder
clone this paste RAW Paste Data
Top