Cod sursa(job #948113)

Utilizator rootsroots1 roots Data 9 mai 2013 15:05:52
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <stdio.h>
#include <iostream>
using namespace std;

#define MAXSIZE 2000000/8/2+1

char p[MAXSIZE];

int Primes_5(int n)
{
    int i, j, nr = 1;

    for (i = 1; ((i * i) << 2) + (i << 2) + 1 <= n; i += 1)
        if ((p[i >> 3] & (1 << (i & 7))) == 0)
            for (j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1)
                p[j >> 3] |= (1 << (j & 7));

    for (i = 1; 2 * i + 1 <= n; ++i)
        if ((p[i >> 3] & (1 << (i & 7))) == 0)
            nr++;
    return nr;
}

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

    int N;
    cin >> N;
    cout << Primes_5(N) << '\n';

    return 0;
}