题解 | #素数判定#
素数判定
https://www.nowcoder.com/practice/5fd9c28b1ce746dd99287a04d8fa9002
只能被1和本身整除的数为素数(质数);0、1、负数为质数;若存在大于根号n的因数,则必存在小于根号n的因数
#include <iostream>
#include "cmath"
using namespace std;
int main() {
int n;
while (cin >> n) { // 注意 while 处理多个 case
// cout << a + b << endl;
string result = "yes";
int bound = sqrt(n);
if (n == 0 || n == 1 || n < 0) result = "no";
for (int i = 2; i <= bound; i++) {
if (n % i == 0) result = "no";
}
cout << result << endl;
}
}
// 64 位输出请用 printf("%lld")