Cod sursa(job #1814258)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 23 noiembrie 2016 19:50:14
Problema Ciurul lui Eratosthenes Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <cstdio>
using namespace std;

int n;
bool prim[100000];

void sieve()
{
    prim[2]=1;
    prim[3]=1;
    for (int x = 1; x*x <= n; x++)

        for (int y = 1; y*y <= n; y++)
        {
            int num = (4 * x * x + y * y);
            if (num <= n && (num % 12 == 1 || num % 12 == 5))
                prim[num] = true;
            num = (3 * x * x + y * y);
            if (num <= n && (num % 12 == 7))
                prim[num] = true;

            if (x > y)
            {
                num = (3 * x * x - y * y);
                if (num <= n && (num % 12 == 11))
                    prim[num] = true;
            }
        }

    for (int i = 5; i*i <= n; i++)
        if (prim[i])
            for (int j = i * i; j <= n; j += i)
                prim[j] = false;

    int k=0;

    for (int i = 2; i <= n; i++)
         if (prim[i])
             k++;

    printf("%d",k);
}

int main()
{
    freopen("ciur.in","r",stdin);
    freopen("ciur.out","w",stdout);
    scanf("%d",&n);
    sieve();
    return 0;
}