You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
664 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
// 1、基本法 判断n是否为素数
bool is_prime(int n) {
if (n < 2)
return false;
for (int i = 2; i < n; ++i)
if (n % i == 0)
return false;
return true;
}
//2 、进阶优化方法
bool is_prime2(int n) {
if (n < 2)
return false;
/**
* 只需要判断到根号n即可这里必须要使用小于等于符号
* 不能仅仅使用小于符号例如判断9
**/
for (int i = 2; i * i <= n; ++i)
if (n % i == 0)
return false;
return true;
}
int main() {
for (int i = 2; i < 1000; i++) {
cout << i << ":" << is_prime2(i) << endl;
}
return 0;
}