1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 bool isPrime[1000000]; 6 int prime[1000000]; 7 int MAX, total, cnt; 8 9 void makePrime() { 10 memset(isPrime,true,sizeof(isPrime)); 11 memset(prime,0,sizeof(prime)); 12 total = cnt = 0; 13 for(int i = 2; i <= MAX; ++i) { 14 if(isPrime[i]) prime[total++] = i; 15 for(int j = 0; j < total && i*prime[j] <= MAX; ++j) { 16 ++cnt; 17 isPrime[i*prime[j]] = false; 18 //i此时不是素数,只是拓展用 19 if(i%prime[j] == 0) break; 20 } 21 } 22 } 23 24 int main(){ 25 while(cin>>MAX){ 26 makePrime(); 27 cout<<total<<' '<<cnt<<endl; 28 //for(int i = 0; i < total; ++i) cout<<prime[i]<<endl; 29 } 30 }
相关文章: