Various functions related to prime numbers.
class PrimeNumber {
public:
// Check if given number is prime or not.
static bool isPrime(unsigned long long num ) {
if(num <= 1) return false;
for( long long i = 2; (long long) i * i <= num; i++)
if( num % i == 0) return false;
return true;
}
};
Describe(PrimeNumber_TestIsPrime)
{
It(TestIsPrimeSmall)
{
Assert::That(false, Equals(PrimeNumber::isPrime(0)));
Assert::That(false, Equals(PrimeNumber::isPrime(1)));
Assert::That(false, Equals(PrimeNumber::isPrime(-1)));
Assert::That(true, Equals(PrimeNumber::isPrime(3)));
Assert::That(true, Equals(PrimeNumber::isPrime(11)));
Assert::That(true, Equals(PrimeNumber::isPrime(57191)));
}
It(TestIsPrimeLarge)
{
Assert::That(true, Equals(PrimeNumber::isPrime(1000000007)));
Assert::That(true, Equals(PrimeNumber::isPrime(2147483647)));
// Takes longer than 12000ms to complete.
//Assert::That(true, Equals(PrimeNumber::isPrime(2305843009213693951)));
}
};