Cod sursa(job #1992476)

Utilizator zanugMatyas Gergely zanug Data 20 iunie 2017 15:47:40
Problema Ciurul lui Eratosthenes Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

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

const int N = 2e6+10;
bool x[N];
int n, ossz;

int main()
{
    fin >> n;
    int gyok = sqrt(n);

    x[0] = x[1] = 1;

    for(int i = 4; i <= n; i += 2)
        x[i] = 1;

    for(int j = 3; j <= gyok && !x[j]; j += 2)
    {
        for(int i = 3*j; i <= n; i += 2 * j)
        {
            x[i] = 1;
        }
    }

    for(int i = 0; i <= n; ++i)
        if(x[i] == 0)
            ++ossz;

    fout << ossz;

    return 0;
}