#define NMAX 65536 #define MAXBITS 32 void isqrt(unsigned int x, unsigned int *outp) { unsigned int m, y, b, a; a = x; m = 0x1 << (MAXBITS-2); y = 0; while (m != 0) { b = y | m; y = y >> 1; if (a >= b) { a = a - b; y = y | m; } m = m >> 2; } *outp = y; } void sieve(unsigned int n, unsigned int *outp) { static unsigned int erat[NMAX]; unsigned int count, m, i, j, x; x = n; for (i = 0; i < NMAX; i++) { erat[i] = 0; } count = 0; erat[0] = 1; erat[1] = 1; isqrt(x, &m); for (i = 2; i <= m; i++) { if (erat[i] == 0) { for (j = i*i; j <= x; j += i) { erat[j] = 1; } } } for (i = 2; i <= x; i++) { if (erat[i] == 0) { count++; } } *outp = count; }