Cod sursa(job #2673148)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 15 noiembrie 2020 21:48:27
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.43 kb
#include <fstream>

const int nmax = 2e6 + 5;

bool ciur[nmax];

void sieve(int n) {
    for(int i=3;i*i<=n;i+=2)
        if (!ciur[i]) {
            for (int j = i * i; j <= n; j += 2 * i) ciur[j] = 1;
        }
}

int main() {
    std::ifstream fin("ciur.in");
    std::ofstream fout("ciur.out");
    int n, ans = 1;
    fin >> n;
    sieve(n);
    for (int i = 3; i <= n; i += 2) ans += 1 - ciur[i];
    fout << ans;
}