Cod sursa(job #1766214)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 27 septembrie 2016 18:24:50
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <bitset>
#include <cmath>
using namespace std;

const int maxLength = 2000000 + 1;

int computeSieve(bitset<maxLength> &isNotPrime, int N)
{
    int i, j, nonPrimes = 1;
    isNotPrime[0] = isNotPrime[1] = 1;
    for (i = 4; i <= N; i += 2)
    {
        isNotPrime[i] = 1;
        nonPrimes++;
    }

    for (i = 3; i <= sqrt(N); i += 2)
        if (!isNotPrime[i])
            for (j = i * i; j <= N; j += i + i)
            {
                if (isNotPrime[j] == 0)
                {
                    nonPrimes++;
                    isNotPrime[j] = 1;
                }
            }

    return N - nonPrimes;
}

int main()
{
    int N;
    bitset<maxLength> isNotPrime;

    ifstream f("ciur.in");
    f >> N;
    f.close();

    ofstream g("ciur.out");
    g << computeSieve(isNotPrime, N);
    g.close();

    return 0;
}