#include <stdio.h>

/*
 * Sieve of Erastosthenes - a standard C benchmark.
 * Try timing the 100 iterations of counting all primes less than 8190.
 * To compile this, use "cc c.sieve"; to run it, just type "sieve".
 */

#define TRUE        1
#define FALSE       0
#define SIZE     8190

static char flags[SIZE+1];

int main(int argc, char *argv[])
{
    unsigned int i, prime, k, count, iter;

    printf("Sieve of Erastosthenes - 100 iterations...");

    for (iter = 1; iter <= 100; iter++)
    {   count = 0;
        for (i = 0; i <= SIZE; i++) flags[i] = TRUE;
        for (i = 0; i <= SIZE; i++)
        {   if (flags[i])
            {   prime = i + i + 3;
                for (k = i+prime; k <= SIZE; k += prime)
                    flags[k] = FALSE;
                count++;
            }
        }
    }
    printf("\nThere are %u primes less than %u\n", count, SIZE);
    return 0;
}
