http://acm.bjtu.edu.cn/problem/detail?pid=1188

百万级素数筛选,果断埃斯托拉尼筛法

View Code
#include <stdio.h>
int prime[1000001];
int ans[1000001]={0,0,1};
int main()
{
    int i,j,n;
    for(i=2;i*i<=1000000;i++)
        if(!prime[i])
            for(j=i;j*i<=1000000;j++)
                prime[j*i]=1;
    for(i=3;i<=1000000;i++)
        if(!prime[i])
            ans[i]=ans[i-1]+1;
        else
            ans[i]=ans[i-1];
    while(~scanf("%d",&n))
        printf("%d\n",ans[n]);
    return 0;
}