about summary refs log tree commit diff
path: root/ntu/pi.c
blob: 0dc8a1e67bd0976281cc8a690b642ef1254907ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main()
{
	long i, j;
	char prime[44712] = {0, 0, [2 ... 44711] = 1};
	for (i = 2; i < 212; i++)
		if (prime[i])
			for (j = i * i; j < 44712; j += i)
				prime[j] = 0;


	long primes[4648];
	j = 0;
	for (i = 0; i < 44712; i++)
		if (prime[i])
			primes[j++] = i;

	long l, r;
	scanf("%ld %ld", &l, &r);

	long imax = (long) sqrt(r);
	for (i = 0; i < 4648; i++)
		if (primes[i] > imax) {
			imax = i;
			break;
		}

	r -= l - 1;
	long *list = malloc(r * 4);
	for (i = 0; i < r; i++)
		list[i] = 1;

	long prime0, l0;
	for (i = 0; i < imax; i++) {
		prime0 = primes[i];
		l0 = (l % prime0) ? (prime0 - (l % prime0)) : 0;
		l0 = (l + l0 > prime0 * prime0) ? l0 : (prime0 * prime0 - l);
		for (j = l0; j < r; j += prime0)
			list[j] = 0;
	}

	long val = 0;
	for (i = 0; i < r; i++)
		if (list[i])
			val++;

	printf("%ld\n", val);

	return 0;
}