PAT (Basic Level) Practice (中文) 1013 数素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cmath> 3 using namespace std; 4 5 const int MAXN = 100000000; 6 int a[MAXN]; 7 bool isprime(int x); 8 int main(){ 9 int n,count=0; 10 for(int i=2;i<MAXN;i++){ 11 if(isprime(i)==true){ 12 a[i] = 1; 13 count++; 14 if(count==10000) printf("i: %d\n",i); 15 } 16 } 17 18 19 20 scanf("%d",&n); 21 return 0; 22 } 23 24 bool isprime(int x){ 25 if(x==1) return false; 26 if(x==2) return true; 27 for(int i=2;i<=sqrt((double)x)+1;i++){ 28 if(x % i==0) return false; 29 30 } 31 return true; 32 }
因此在104729时有10000个素数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cmath> 3 using namespace std; 4 const int MAXN = 10001; 5 int p[MAXN],m,n; 6 bool isprime(int x); 7 void init(); 8 void deal(); 9 int main(){ 10 init(); 11 scanf("%d %d",&m,&n); 12 deal(); 13 return 0; 14 } 15 bool isprime(int x){ 16 if(x==1) return false; 17 else if(x==2) return true; 18 19 for(int i=2;i<=sqrt((double)x)+1;i++){ 20 if(x%i==0) return false; 21 } 22 return true; 23 } 24 void init(){ 25 int count=1; 26 for(int i=2;i<10000000;i++ ){ 27 if(isprime(i)){ 28 p[count++] = i; 29 if(count==10001) break; 30 } 31 } 32 } 33 void deal(){ 34 for(int i=m;i<=n;i++){ 35 printf("%d",p[i]); 36 if(i!=n && (i-m+1)%10!=0) printf(" "); 37 else printf("\n"); 38 } 39 }