Cod sursa(job #2228539)

Utilizator inquisitorAnders inquisitor Data 4 august 2018 10:15:45
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <cstdio>

using namespace std;

int v[(2000000 >> 6) + 10];

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

    int n;
    scanf ("%d", &n);

    for (int i = 1; ((i * i) << 1) + (i << 1) <= n; ++i)
        if (!(v[i >> 5] & (1 << (i & 31))))
            for (int j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1)
                v[j >> 5] |= (1 << (j & 31));

    int nr = 1;
    for (int i = 1; 2 * i + 1 <= n; ++i)
        if (!(v[i >> 5] & (1 << (i & 31))))
        {
            ++nr;
            //printf ("%d ", 2 * i + 1);
        }

    printf ("%d\n", nr);

    return 0;
}