Cod sursa(job #2638112)

Utilizator gabib97Gabriel Boroghina gabib97 Data 26 iulie 2020 23:03:16
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int sieve(int n) {
    vector<bool> notPrime(n + 1);
    int nrPrimes = 0;

    notPrime[0] = notPrime[1] = true;
    for (int i = 2; i <= n; i++)
        if (!notPrime[i])
            for (int j = 2 * i; j <= n; j += i)
                notPrime[j] = true;

    for (int i = 2; i <= n; i++)
        if (!notPrime[i]) nrPrimes++;
    return nrPrimes;
}

int main() {
    ifstream fin("ciur.in");
    ofstream fout("ciur.out");

    int n;
    fin >> n;

    fout << sieve(n);
    return 0;
}