Cod sursa(job #613385)

Utilizator ucnahHancu Andrei ucnah Data 23 septembrie 2011 19:41:12
Problema Ciurul lui Eratosthenes Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <cstdio>

using namespace std;
int a[2000005];
int main()
{
    freopen("ciur.in","r",stdin);
    freopen("ciur.out","w",stdout);
    int n,nr=0;
    scanf("%d",&n);
    int i;
    int p=2;
    while(p*p<=n)
    {
        for(i=p+p;i<=n;i+=p)
        {
            a[i]=-1;
        }
        for(int j=p+1;j<=n;j++)
        {
            if(a[j]!=-1)
            {
                p=j;
                break;
            }
        }
    }
    for(int j=2;j<=n;j++)
        if(a[j]==0)
            nr++;
        printf("%d",nr);

    return 0;
}