mirror of
https://github.com/cc65/cc65.git
synced 2024-10-31 20:06:11 +00:00
0bb81e5c36
git-svn-id: svn://svn.cc65.org/cc65/trunk@784 b7a2c559-68d2-44c3-8de9-860c34a00d81
87 lines
1.7 KiB
C
87 lines
1.7 KiB
C
/*
|
|
* Calculate all primes up to a specific number.
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#include <time.h>
|
|
#include <conio.h>
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
/* Data */
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
#define COUNT 8192 /* Up to what number? */
|
|
#define SQRT_COUNT 91 /* Sqrt of COUNT */
|
|
|
|
static unsigned char Sieve[COUNT];
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
/* Code */
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
int main (void)
|
|
{
|
|
/* Clock variable */
|
|
clock_t Ticks;
|
|
|
|
/* This is an example where register variables make sense */
|
|
register unsigned char* S;
|
|
register unsigned I;
|
|
register unsigned J;
|
|
|
|
/* Read the clock */
|
|
Ticks = clock();
|
|
|
|
/* Execute the sieve */
|
|
I = 2;
|
|
while (I < SQRT_COUNT) {
|
|
if (Sieve[I] == 0) {
|
|
/* Prime number - mark multiples */
|
|
S = &Sieve[J = I*2];
|
|
while (J < COUNT) {
|
|
*S = 1;
|
|
S += I;
|
|
J += I;
|
|
}
|
|
}
|
|
++I;
|
|
}
|
|
|
|
/* Calculate the time used */
|
|
Ticks = clock() - Ticks;
|
|
|
|
/* Print the time used and wait for a key */
|
|
printf ("Time used: %lu ticks\n", Ticks);
|
|
printf ("Press Q to quit, any other key for list\n");
|
|
if (toupper (cgetc()) == 'Q') {
|
|
exit (EXIT_SUCCESS);
|
|
}
|
|
|
|
/* Print the result */
|
|
for (I = 2; I < COUNT; ++I) {
|
|
if (Sieve[I] == 0) {
|
|
printf ("%4d\n", I);
|
|
}
|
|
if (kbhit() && toupper (cgetc()) == 'q') {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
|
|
|