OJ-Problems-Source/.ACM-Templates/isPrime.cpp
2016-08-11 09:07:22 +08:00

21 lines
396 B
C++

//Written by Coffee. 判断素数
bool isPrime(int num)
{
if (num == 2 || num == 3)
{
return true;
}
if (num % 6 != 1 && num % 6 != 5)
{
return false;
}
for (int i = 5; i*i <= num; i += 6)
{
if (num % i == 0 || num % (i+2) == 0)
{
return false;
}
}
return true;
}