mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create isPrime.cpp
This commit is contained in:
parent
0d0d02cf94
commit
c7c23ae7f9
20
.ACM-Templates/isPrime.cpp
Normal file
20
.ACM-Templates/isPrime.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user