Saturday, July 7, 2007

Prime Number

/* Following method will check whether a is a prime or not */
public static boolean isPrime(int a) {
boolean prime = true;

for (int counter = 2; counter < a; counter++)
if (a % counter == 0)
prime = false;

return (prime);
}
/* Following method will return highest prime number closest to parameter n */
public static int getPrime(int n) {
int bigPrime = 0;
for (int counter = 2; counter <= n; counter++)
if (isPrime(counter))
bigPrime = counter > bigPrime ? counter : bigPrime;
return bigPrime;
}

No comments: