Cod sursa(job #432045)

Utilizator AndrewTheGreatAndrei Alexandrescu AndrewTheGreat Data 1 aprilie 2010 19:31:58
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <iostream>

using namespace std;

bool sir[2000010];

int ciur(int x)
{
    int nr,i,k;
    nr=(x>>1)+(x&1);
    for(i=4;i<=x;i+=2)
        sir[i]=true;
    for(i=3;i*i<=x;i+=2)
    {
        k=i*i;
        while(k<=x)
        {
            if(sir[k]==false)
            {
                nr--;
                sir[k]=true;
            }
            k+=i;
        }
    }
    return nr;
}

int main()
{
    freopen ("ciur.in","r",stdin);
    freopen ("ciur.out","w",stdout);

    int N;
    scanf("%d",&N);
    printf("%d\n",ciur(N));
    return 0;
}