Cod sursa(job #1017565)

Utilizator romykPrehari Romica romyk Data 27 octombrie 2013 22:12:13
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include<stdio.h>
const int Nmax = 2000070/8+1;
int N, rez;

char p[Nmax];

//(p[i>>3]&(1<<(i&7))) == 0 if 2*i + 1 is prime

long eratostene(int n) {
    int i, nr = 1;
    for(i = 1; ((i * i) << 1) + (i<< 1) <= n;i+=1 ) {
        if((p[i >> 3] & (1 << (i & 7))) == 0) {

            for (int j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1) {
                p[j >> 3] |= (1 << (j & 7));
            }
        }
    }

    for(int 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);
    scanf("%i",&N);
    {rez = eratostene(N);
 printf("%d",rez);}

    return 0;
}