Cod sursa(job #1243539)

Utilizator js3292618Andrei Mihai js3292618 Data 16 octombrie 2014 00:19:38
Problema Ciurul lui Eratosthenes Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 0.65 kb
#include <stdio.h>
#include <limits.h> /* CHAR_BIT */

#define NMAX 2000000 / CHAR_BIT
#define IN "ciur.in"
#define OUT "ciur.out"

#define GET_BIT(x) (ciur[(x + 1) / CHAR_BIT] & (1 << (x & (CHAR_BIT - 1))))
#define SET_BIT(x) (ciur[(x + 1) / CHAR_BIT] |= (1 << (x & (CHAR_BIT - 1))))

typedef unsigned long Int;

static char ciur[NMAX];

Int
prime(Int n)
{
	Int i, j, cnt = 0;

	for (i = 2; i * i <= n; i++)
		if (GET_BIT(i) == 0) {
		    ++cnt;
			for (j = i * i; j <= n; j += i)
				SET_BIT(j);
		}

	return cnt;
}

int main(void)
{
	Int n;

	freopen(IN, "r", stdin);
	freopen(OUT, "w", stdout);

	scanf("%lu", &n);
	printf("%lu\n", prime(n));

	return 0;
}