--Check whether an integer is a prime or not
--Least Divisor
leastDivisor::Integer->Integer->Integer
leastDivisor x y | mod y x == 0 = x
| x^2 > y = y
| otherwise = leastDivisor (x+1) y
--Check prime number
isPrime::Integer->Bool
isPrime y = leastDivisor 2 y == y
public static boolean prime(int d, int n)
{
if(n == 2)
return true;
else if(n % d == 0)
return false;
else if(d*d <= n)
return prime(d+1, n);
else
return true;
}
public static boolean isPrimeLoop(int n)
{
if( n == 2)
return true;
for(int d=2; d*d <= n; d++)
{
if(n % d == 0)
return false;
}
return true;
}