Cod sursa(job #2069900)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 18 noiembrie 2017 22:23:57
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>
#define nMax 2000010
using namespace std;
ifstream in("ciur.in");
ofstream out("ciur.out");
int n;
bool primes[nMax];

int eratosthenes(int n)
{
    int nr = 1;
    for(int i = 1; (i<<1) +1 <= n; ++i)
    {
        if(!primes[i])
        {
            nr++;
            for(int j = (i<<1) + i+1; (j<<1) + 1 <= n; j += (i<<1) +1)
                primes[j] = 1;
        }
    }
    return nr;
}

int main()
{
    in >> n;
    out << eratosthenes(n) << '\n';
    return 0;
}