Cod sursa(job #1975052)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 29 aprilie 2017 19:46:13
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>

using namespace std;

const int nMax = 2000005;

int n;
bool compus[nMax];

void ciur()
{
    for(int i = 4; i <= n; i += 2)
        compus[i] = true;
    for(int i = 3; i * i <= n; i += 2)
        for(int j = i * i; j <= n; j += i)
            compus[j] = true;
}

void afisare()
{
    ofstream out("ciur.out");
    int rasp = 0;
    for(int i = 2; i <= n; ++i)
        if(compus[i] == false)
            ++rasp;
    out << rasp;
    out.close();
}

int main()
{
    ifstream in("ciur.in");
    in >> n;
    in.close();
    ciur();
    afisare();
    return 0;
}