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>
|
|
|
|
|
|
|
|
|
2009-10-03 11:17:39 +00:00
|
|
|
/* Workaround missing clock stuff */
|
2013-06-17 19:34:08 +00:00
|
|
|
#ifdef __APPLE2__
|
2013-05-09 11:56:54 +00:00
|
|
|
# define clock() 0
|
|
|
|
# define CLOCKS_PER_SEC 1
|
2009-10-03 11:17:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-09 11:56:54 +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];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-14 12:55:15 +00:00
|
|
|
#pragma static-locals(1);
|
2001-10-13 15:46:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2013-05-09 11:56:54 +00:00
|
|
|
register unsigned I;
|
|
|
|
register unsigned J;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
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) {
|
2013-05-09 11:56:54 +00:00
|
|
|
if (Sieve[I] == 0) {
|
|
|
|
/* Prime number - mark multiples */
|
2009-08-28 17:21:58 +00:00
|
|
|
J = I*2;
|
2013-05-09 11:56:54 +00:00
|
|
|
S = &Sieve[J];
|
|
|
|
while (J < COUNT) {
|
|
|
|
*S = 1;
|
|
|
|
S += I;
|
|
|
|
J += I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++I;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
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') {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Print the result */
|
2009-07-30 19:25:59 +00:00
|
|
|
J = 0;
|
2013-05-09 11:56:54 +00:00
|
|
|
for (I = 2; I < COUNT; ++I) {
|
|
|
|
if (Sieve[I] == 0) {
|
|
|
|
printf ("%4d\n", I);
|
2009-07-30 19:25:59 +00:00
|
|
|
if (++J == 23) {
|
|
|
|
printf ("Q to quit, any other key continues\n");
|
|
|
|
if (ReadUpperKey () == 'Q') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
J = 0;
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
if (kbhit() && ReadUpperKey == 'Q') {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|