朴素筛与线性筛

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+5;

bool st[N];
int cnt;
int primes[N];

void get_primes(int n){ // O(nlogn) 朴素筛法
	for(int i = 2; i <= n; ++i){
		if(!st[i]){
			primes[cnt++] = i;
		}
		for(int j = i + i; j <= n; j += i){
			st[j] = true;
		}
	}
}


void get_primes(int n){ // O(n) 线性筛
	for(int i = 2; i <= n; ++i){
		if(!st[i]) primes[cnt++] = i;
		for(int j = 0; primes[j] <= n/i; ++j){
			st[primes[j] * i] = true;
			if(i % primes[j] == 0) break;
		}
	}
}
---- suffer now and live the rest of your life as a champion ----
原文地址:https://www.cnblogs.com/popodynasty/p/14675361.html