Skip to content

Commit 21d0275

Browse files
authored
朴素的质数测试(O(sqrt(n)))
1 parent 96fdbf8 commit 21d0275

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Prime-Check(Naive).cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <cstdio>
2+
3+
using namespace std;
4+
5+
bool check(int x)
6+
{
7+
for (int i = 2; i * i <= x; i++)
8+
{
9+
if (x % i == 0)
10+
{
11+
return false;
12+
}
13+
}
14+
return true;
15+
}
16+
17+
int main()
18+
{
19+
int n;
20+
while (scanf("%d", &n) > 0)
21+
{
22+
printf("%s\n", check(n) == true ? "YES" : "NO");
23+
}
24+
return 0;
25+
}

0 commit comments

Comments
 (0)