来源:http://my.oschina.net/dianpaopao/blog/83844
#include<stdio.h> #include<stdlib.h> #define NMAX 10000000 #define SQRN 10000 int main() { int n; scanf("%d",&n); static char a[NMAX]; unsigned long k,i; for(k=2;k<=SQRN;k++) if(!a[k]) for(i=2*k;i<n;i+=k) a[i]=1; for(i=2;i<n;i++) if(!a[i]) printf("%ld\n",i); return 0; }
PS:出处下面的评论更强大,还在理解中ing...