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;
const int N = 10000810;
int primes[N], cnt;
bool st[N];
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i; //没有被标识过,就增加到质数数组中去
// 线性筛法核心:N 只会被它最小的质因子筛掉
// 从小到大枚举所有可能满足条件的质数
for (int j = 0; primes[j] <= n / i; j++) {
st[primes[j] * i] = true; //从小向大,所以这里可以这么写primes[j],标识st[primes[j] * i]已经标识过了
//最小质因子就可以了,其它质因子放弃掉,看上面的链接里有原因说明
if (i % primes[j] == 0) break;
}
int main() {
get_primes( 1e6 + 10);
for (int i = 0; i < cnt; i++) cout << primes[i] << " ";
return 0;