2000-05-28 13:40:48 +00:00
|
|
|
/*
|
|
|
|
* Calculate all primes up to a specific number.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-14 14:45:04 +00:00
|
|
|
#include <stdlib.h>
|
2000-05-28 13:40:48 +00:00
|
|
|
#include <stdio.h>
|
2001-07-14 14:45:04 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <time.h>
|
2000-05-28 13:40:48 +00:00
|
|
|
#include <conio.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Data */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-28 17:21:58 +00:00
|
|
|
#define COUNT 16384 /* Up to what number? */
|
|
|
|
#define SQRT_COUNT 128 /* Sqrt of COUNT */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
static unsigned char Sieve[COUNT];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2003-12-21 12:51:59 +00:00
|
|
|
/* Code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-13 15:46:03 +00:00
|
|
|
#pragma staticlocals(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-30 19:25:59 +00:00
|
|
|
static char ReadUpperKey (void)
|
|
|
|
/* Read a key from console, convert to upper case and return */
|
|
|
|
{
|
|
|
|
return toupper (cgetc ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-12-21 12:51:59 +00:00
|
|
|
int main (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2001-07-14 14:45:04 +00:00
|
|
|
/* Clock variable */
|
|
|
|
clock_t Ticks;
|
2009-07-30 19:25:59 +00:00
|
|
|
unsigned Sec;
|
|
|
|
unsigned Milli;
|
2001-07-14 14:45:04 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* This is an example where register variables make sense */
|
|
|
|
register unsigned char* S;
|
|
|
|
register unsigned I;
|
|
|
|
register unsigned J;
|
|
|
|
|
2003-12-21 12:51:59 +00:00
|
|
|
/* Output a header */
|
|
|
|
printf ("Sieve benchmark - calculating primes\n");
|
|
|
|
printf ("between 2 and %u\n", COUNT);
|
|
|
|
printf ("Please wait patiently ...\n");
|
|
|
|
|
2001-07-14 14:45:04 +00:00
|
|
|
/* Read the clock */
|
|
|
|
Ticks = clock();
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Execute the sieve */
|
|
|
|
I = 2;
|
|
|
|
while (I < SQRT_COUNT) {
|
|
|
|
if (Sieve[I] == 0) {
|
|
|
|
/* Prime number - mark multiples */
|
2009-08-28 17:21:58 +00:00
|
|
|
J = I*2;
|
|
|
|
S = &Sieve[J];
|
2000-05-28 13:40:48 +00:00
|
|
|
while (J < COUNT) {
|
|
|
|
*S = 1;
|
|
|
|
S += I;
|
|
|
|
J += I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
2001-07-14 14:45:04 +00:00
|
|
|
/* Calculate the time used */
|
|
|
|
Ticks = clock() - Ticks;
|
2009-07-30 19:25:59 +00:00
|
|
|
Sec = (unsigned) (Ticks / CLOCKS_PER_SEC);
|
|
|
|
Milli = ((Ticks % CLOCKS_PER_SEC) * 1000) / CLOCKS_PER_SEC;
|
2001-07-14 14:45:04 +00:00
|
|
|
|
2001-10-13 15:45:04 +00:00
|
|
|
/* Print the time used */
|
2009-07-30 19:25:59 +00:00
|
|
|
printf ("Time used: %u.%03u seconds\n", Sec, Milli);
|
|
|
|
printf ("Q to quit, any other key for list\n");
|
2001-10-13 15:46:03 +00:00
|
|
|
|
2001-10-13 15:45:04 +00:00
|
|
|
/* Wait for a key and print the list if not 'Q' */
|
2009-07-30 19:25:59 +00:00
|
|
|
if (ReadUpperKey () != 'Q') {
|
2001-10-13 15:45:04 +00:00
|
|
|
/* Print the result */
|
2009-07-30 19:25:59 +00:00
|
|
|
J = 0;
|
2001-10-13 15:45:04 +00:00
|
|
|
for (I = 2; I < COUNT; ++I) {
|
|
|
|
if (Sieve[I] == 0) {
|
2009-07-30 19:25:59 +00:00
|
|
|
printf ("%4d\n", I);
|
|
|
|
if (++J == 23) {
|
|
|
|
printf ("Q to quit, any other key continues\n");
|
|
|
|
if (ReadUpperKey () == 'Q') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
J = 0;
|
|
|
|
}
|
2001-10-13 15:45:04 +00:00
|
|
|
}
|
2009-07-30 19:25:59 +00:00
|
|
|
if (kbhit() && ReadUpperKey == 'Q') {
|
2001-10-13 15:45:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2001-07-14 14:45:04 +00:00
|
|
|
return EXIT_SUCCESS;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|