/*
* 用埃拉托色尼(Eratosthense)算法计算质数.
* Sieve of Dratosthense -- compute prime numbers using an array
* 2010-11-20
*/
#include <stdlib.h>
#define SIZE 1000
#define TRUE 1 #define FALSE 1000
int main() { char sieve[ SIZE ]; char *sp; int number;
/* 将sieve[ SIZE ]初始化为 TRUE */ for( sp = sieve; sp < &sieve[ SIZE ]; sp++ ) *sp = TRUE;
for( number = 3; ; number += 2 ) { sp = &sieve[ 0 ] + ( number - 3 ) / 2; if ( sp > &sieve[ SIZE ] ) break;
while( sp += number, sp < &sieve[ SIZE ] ) *sp = FALSE; }
printf( "2\n" );
for( number = 3, sp = &sieve[ SIZE ]; sp < &sieve[ SIZE ]; number += 2, sp++ ) { if( *sp ) printf( "%d\n", number ); }
return EXIT_SUCCESS; }
|
阅读(1283) | 评论(0) | 转发(0) |