题解 | #素数判定#
素数判定
https://www.nowcoder.com/practice/5fd9c28b1ce746dd99287a04d8fa9002
#include<iostream>
#include<cmath>
using namespace std;
//例题6.7 素数判定
int isPrime(int num) {
if (num == 1) {
return 0;
}
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
int main()
{
int n;
while (cin >> n) {
if (isPrime(n) == 1) {
cout << "yes" << endl;
}
else {
cout << "no" << endl;
}
}
return 0;
}
// 64 位输出请用 printf("%lld")