#include <iostream> using namespace std; void find_primer(bool **primers, int n) { if(n <= 0) { cout<<"error!"<<endl; return; } *primers = new bool[n+1]; for(int i = 0; i < n+1; i++) { (*primers)[i] = true; } (*primers)[1] = false; if(n >= 2) { int p,q,r = n>>1; for(p = 2; p <= r; p++) { for(q = 2; q<=p; q++) { if(p * q > n) { break; } (*primers)[p*q] = false; } } } } int main() { bool *primers; int n; cin>>n; find_primer(&primers, n); for(int i=1; i<=n; i++) { if(primers[i] == true) { cout<<i<<endl; } } return 0; }
以空间换时间高效求素数算法
最新推荐文章于 2021-05-19 17:09:31 发布