
primes all ints
By: a guest on
Nov 11th, 2015 | syntax:
Java | size: 1.31 KB | views:
83 | expires: Never
package primeFinder;
import java.util.Scanner;
public class PrimeFinder2 {
public static void main(String[] args){
double timeS, timeE;
double timeT;
Scanner input=new Scanner(System.in);
for(;;){
System.out.println("enter a positive integer greater than 1 to find all the primes less than it enter \"0\" to qutit");
long n=input.nextLong();
if (n<0||n==1){
System.out.println("enter a number greater than 1");
}
else if (n==0){
System.exit(0);
}
else{
System.out.println("valid number entered primes up to "+n+" are:");
timeS=System.currentTimeMillis();
primesToN(n);
timeE=System.currentTimeMillis();
timeT=(timeE-timeS)/1000;
System.out.println("it took "+timeT+" S to calculate primes up to "+n);
}
}
}//end of main
public static void primesToN(Long n){
System.out.print("2, ");
boolean print=false;
int count=0;
for(long i=3;i<=n;i++){
for(int x=2;x<i;x++){
if(i%x==0){
print=false;
break;
}
else{
print=true;
}
}
if(print&&count!=4&&i!=n){
System.out.print(i+", ");
count++;
}
else if(print&&i!=n){
System.out.println(i+", ");
count=0;
}
print=false;
}
}//end of primesToN
}//end of prime finder