Cod sursa(job #1238307)

Utilizator predator5047Butiu Alexandru Octavian predator5047 Data 6 octombrie 2014 18:23:16
Problema Ciurul lui Eratosthenes Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.16 kb
#include <stdio.h>
#include <math.h>

int n, c;
char v[2000002];

void sieve_of_atkin() {
    int i, j, x, n_root = (int)sqrt(n) + 1;
    for (i = 1; i <= n_root; i++) {
        for (j = 1; j <= n_root; j++) {
            x = 4 * i * i + j * j;
            if (x <= n && (x % 12 == 1 || x % 12 == 5)) {
                v[x] = !v[x];
            }
            x = 3 * i * i + j * j;
            if (x <= n && x % 12 == 7) {
                v[x] = !v[x];
            }
            x = 3 * i * i - j * j;
            if (i > j && x <= n && x % 12 == 11) {
                v[x] = !v[x];
            }
        }
    }
    for (i = 5; i <= n_root; i++) {
        if (v[i]) {
            for (j = i * i; j <= n; j += i * i) {
                v[j] = 0;
            }
        }
    }
    c = 2;
    for (i = 5; i <= n; i++) {
        if (v[i]) {
            c++;
            //printf("%d\n", i);
        }
    }
}

int main() {
    FILE *in, *out;

    in = fopen("ciur.in", "r");
    fscanf(in, "%d", &n);

    sieve_of_atkin();

    out = fopen("ciur.out", "w");

    fprintf(out, "%d", c);

    fclose(in);
    fclose(out);

    return 0;
}