home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1996 September
/
pcwk_09_96.iso
/
demo
/
wgelectr
/
pk51demo
/
files.2
/
EXAMPLES
/
SSIEVE
/
SIEVE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-06-08
|
1KB
|
47 lines
/*------------------------------------------------------------------------------
SIEVE.C: Eratosthenes Sieve Prime Number Program in C from Byte Jan 1983.
------------------------------------------------------------------------------*/
#include <stdio.h>
#include <reg51.h>
#define TRUE 1
#define FALSE 0
#define SIZE 8190
xdata char flags[SIZE+1];
void main()
{
register int i,k;
int prime,count,iter;
SCON = 0x52; /* SCON */
TMOD = 0x20; /* TMOD */
TCON = 0x69; /* TCON */
TH1 = 0xf3; /* TH1 */
printf("10 iterations\n");
for (iter = 1; iter <= 10; iter++) /* do program 10 times */
{
count = 0; /* initialize prime counter */
for (i = 0; i <= SIZE; i++) /* set all flags true */
flags[i] = TRUE;
for (i = 0; i <= SIZE; i++)
{
if (flags[i]) /* found a prime */
{
prime = i + i + 3; /* twice index + 3 */
for (k = i + prime; k <= SIZE; k += prime)
flags[k] = FALSE; /* kill all multiples */
count++; /* primes found */
}
}
}
printf("%d primes.\n",count); /*primes found in 10th pass */
while (1);
}