Cod sursa(job #2775762)

Utilizator radu.z5Zamfirescu Radu Ioan radu.z5 Data 16 septembrie 2021 23:08:55
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#include <cmath>
#include <iostream>

using namespace std;

int eratostene(int n) {
    bool primes[n + 1];
    primes[0] = primes[1] = false;
    int i, j;
    for (i = 2; i <= n; i++)
        primes[i] = true;

    int threshold = (int) sqrt(n);
    int noPrimes = 0;

    for (i = 2; i <= threshold; i++) {
        if (!primes[i])
            continue;
        ++noPrimes;
        for (j = (i << 1); j <= n; j += i)
            if (primes[j])
                primes[j] = false;
    }

    for (i = threshold + 1; i <= n; i++)
        if (primes[i])
            ++noPrimes;
    return noPrimes;
}

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

    int n;
    in >> n;

    out << eratostene(n);
    
    in.close();
    out.close();
    return 0;
}