线性筛法求素数
作者:互联网
时间复杂度为 O(n)!
#include<bits/stdc++.h> using namespace std; const int N = 10e6 + 10; int primes[N]; bool book[N]; void get_primes(){ int cnt = 0; for(int i = 2; i <= N; i++){ if(!book[i]) primes[++cnt] = i; for(int j = 1; i * primes[j] <= N; j++){ book[i * primes[j]] = 1; if(i % primes[j] == 0)break; } } } int main(){ get_primes(); //检查: for(int i = 1; i < 100; i++){ printf("%d ", primes[i]); } return 0; }
标签:std,10,cnt,const,筛法,int,素数,线性,primes 来源: https://www.cnblogs.com/rongrongrong/p/16323994.html