//使用Eratosthenes 方法找出指定范围内的所有质数
//貌似是老外写的 ,真心看不懂。。。
#include <stdlib.h>
#define SIZE 20
#define TRUE 1
#define FALSE 0
int main()
{
char sieve[SIZE];/*the sieve*/
char *sp; /*pointer to access the sieve*/
int number; /*number we're computing*/
/*
** Set the entire sieve to TRUE
*/
for(sp = sieve;sp<&sieve[SIZE];)
*sp++ = TRUE;
/*Process each number from 3 to as many as the sieve holds.(Note:the
** loop is terminated from inside.)
*/
for(number =3;;number+=2){
/**
** set the pointer to the proper element in the sieve,and
** stop the loop if we're gone too far.
*/
sp = &sieve[0] + (number-3)/2;
if(sp>=&sieve[SIZE])
break;
/*
**Now advance the pointer by multiples of the number and set
** each subsequent entry FALSE
*/
while(sp += number,sp<&sieve[SIZE])
*sp =FALSE;
}
printf("2\t");
for(number =3,sp =&sieve[0];
sp<&sieve[SIZE];
number +=2, sp++){
if(*sp)
printf("%d\t",number);
}
system("pause");
return EXIT_SUCCESS;
}
//貌似是老外写的 ,真心看不懂。。。
#include <stdlib.h>
#define SIZE 20
#define TRUE 1
#define FALSE 0
int main()
{
char sieve[SIZE];/*the sieve*/
char *sp; /*pointer to access the sieve*/
int number; /*number we're computing*/
/*
** Set the entire sieve to TRUE
*/
for(sp = sieve;sp<&sieve[SIZE];)
*sp++ = TRUE;
/*Process each number from 3 to as many as the sieve holds.(Note:the
** loop is terminated from inside.)
*/
for(number =3;;number+=2){
/**
** set the pointer to the proper element in the sieve,and
** stop the loop if we're gone too far.
*/
sp = &sieve[0] + (number-3)/2;
if(sp>=&sieve[SIZE])
break;
/*
**Now advance the pointer by multiples of the number and set
** each subsequent entry FALSE
*/
while(sp += number,sp<&sieve[SIZE])
*sp =FALSE;
}
printf("2\t");
for(number =3,sp =&sieve[0];
sp<&sieve[SIZE];
number +=2, sp++){
if(*sp)
printf("%d\t",number);
}
system("pause");
return EXIT_SUCCESS;
}