Cod sursa(job #1243542)

Utilizator js3292618Andrei Mihai js3292618 Data 16 octombrie 2014 00:20:18
Problema Ciurul lui Eratosthenes Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.69 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;

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

	for (cnt = 0, i = 2; i <= n; i++)
		if (GET_BIT(i) == 0)
			++cnt;

	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;
}