Cod sursa(job #948109)

Utilizator rootsroots1 roots Data 9 mai 2013 15:03:03
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 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; 2*(2*i*i+2*i)+1 <= n; i ++)
        if ((p[i>>3] & (1<<(i&7))) == 0)
            for (j = 2*i*i+2*i; 2*j+1 <= n; j += 2*i+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;
}