求素数(筛选法)
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1468&cid=1142
View Code
1 #include<stdio.h> 2 #define N 1000100 3 int a[N] ; 4 int main() 5 { 6 int i, j, count, n ; 7 while(scanf("%d", &n), n!=0) 8 { 9 a[0] = a[1] = 1 ; 10 count = 0 ; 11 for(i=2;i<n;i++) 12 if(a[i]!=1) 13 for(j=i+i;j<n;j+=i) 14 a[j]=1; 15 for(i=2;i<n;i++) 16 if(!a[i]) 17 { 18 count++ ; 19 } 20 printf("%d\n", count) ; 21 } 22 return 0; 23 }