primes/primes.java

36 lines
624 B
Java

// Java version of a Prime Number Finder
// haplo@mindstab.net
class primes {
public static void main(String[] s)
{
if(s.length < 1) {
System.out.println("Useage: java primes [Max Num]\n");
System.exit(1);
}
int max = Integer.valueOf(s[0]).intValue();
if(max < 1) {
System.out.println("Invalid Max Num\n");
System.exit(1);
}
for(int i =3; i<=max; i+=2)
{
int maxTest = (int)Math.sqrt(i);
boolean isPrime = true;
for(int p = 3; p<= maxTest; p+=2)
{
if( (i%p) == 0)
{
isPrime = false;
break;
}
}
if(isPrime)
{
System.out.println(i);
}
}
}
}