#include #define ZERO 0 #define ONE 1 #define TWO 2 #define PRIME '0' #define COMPOSITE '1' #define TOPLIMIT 10000000 #define SQRT 3162 /* */ /* sieve.c--Eratosthenes sieve */ /* */ /* no input */ /* */ /* output is sent to stdout */ main () { char boole[TOPLIMIT]; int i, j, count; count = ZERO; for (i=ONE; i <= TOPLIMIT; ++i) boole[i] = PRIME; for (i=TWO; i <= SQRT; ++i) { if (boole[i] == PRIME) { count += ONE; printf ("%8d\n",i); for (j = i+i; j <= TOPLIMIT; j += i) boole[j] = COMPOSITE; } } for (i = SQRT+ONE; i <= TOPLIMIT; ++i) if (boole[i] == PRIME) { count += ONE; printf ("%8d\n", i); } printf ("number of primes %d\n", count); }