/*
* Sieve of Dratosthense -- compute prime numbers using a bit array
* 2010-11-20
*/
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include "bitarray.h" // 在第五章编程练习中开发的位数组函数
#define MAX_VALUE 10000 #define MAX_BIT_NUMBER ( ( MAX_VALUE - 3 ) / 2 ) #define SIZE ( MAX_BIT_NUMBER / CHAR_BIT + 1 )
int main(void) { char sieve[ SIZE ]; int number; int bit_number; char *sp;
for( sp = sieve; sp < &sieve[ SIZE ]; ) *sp++ = 0xff;
for( number =3; number <= MAX_VALUE; number += 2 ) { bit_number = ( number - 3 ) / 2;
if( !test_bit( sieve, bit_number ) ) continue; }
printf( "2\n" ); for( bit_number = 0,number = 3; number <= MAX_VALUE; bit_number += 1, number += 2 ) { if( test_bit( sieve, bit_number ) ) printf( "%d\n", number ); }
return EXIT_SUCCESS; }
|
阅读(1355) | 评论(0) | 转发(0) |