Create isPrime.cpp

This commit is contained in:
KiritoTRw 2016-08-11 09:07:22 +08:00 committed by GitHub
parent 0d0d02cf94
commit c7c23ae7f9

View File

@ -0,0 +1,20 @@
//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;
}