Cod sursa(job #1819358)

Utilizator zeboftwAlex Mocanu zeboftw Data 30 noiembrie 2016 15:47:34
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>

using namespace std;

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

bool ciur[1000000];

// (2 * i + 1) * (2 * i + 1) = (2 * (2 * i * i) + 2 * i + 2 * 1 + 1) = (2 * (2 * i * i) + 2 * 2 * i + 1) = 2 * (2 * i * i + 2 * i) + 1

int main()
{
    int n, nr = 1;
    ciur[0] = 1;

    fin >> n;

    for (int i=1; ((i * i) << 1) + (i << 1) <= n; i++)
    {
        if (!ciur[i])
        {
            nr++;
            for (int j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i<<1)+1)
                ciur[j] = 1;
        }
    }


    fout << nr;

    return 0;
}