筛质数
(朴素筛法)将每个质数的倍数全部筛掉
下面是完整的代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1000010;
int primes[N];
bool st[N];
int n, cnt;
void get_primes(int x)
{
for (int i = 2; i <= n; i ++ )
{
if (st[i]) continue;
primes[cnt ++ ] = i;
for (int j = i + i; j <= n; j += i)
{
st[j] = true;
}
}
}
int main()
{
cin >> n;
get_primes(n);
cout << cnt;
return 0;
}
姐姐好厉害!
谢谢啦
姐姐好厉害!
把内层
for
循环换个位置优化成埃氏筛法吧~