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 true;
}
//2 、进阶优化方法
bool is_prime2(int n) {
/**
* 只需要判断到根号n即可,这里必须要使用小于等于符号
* (不能仅仅使用小于符号,例如判断9)
**/
for (int i = 2; i * i <= n; ++i)
int main() {
for (int i = 2; i < 1000; i++) {
cout << i << ":" << is_prime2(i) << endl;
return 0;