- #include <stdio.h>
- #include <math.h>
- #define TRUE 1
- #define FALSE 0
- #define MAX_SIZE 1000
- #define PRINT 1
- int find_prime( char * array );
- int find_prime1( char * array );
- int count_number( char *array );
- void print_prime( char *array );
- int main()
- {
- char number[MAX_SIZE];
- int i,count_one , count_two;
- for(i = 0; i <= MAX_SIZE ; i++){
- if( i == 0 || i == 1)
- number[i] = FALSE;
- else
- number[i] = TRUE;
- }
- #ifdef PRINT
- count_one = find_prime(number);
- printf("find_prime 查找质数个数:%d\n",count_one);
- #else
- count_two = find_prime1(number);
- printf("find_prime1 查找质数个数:%d\n",count_two);
- #endif
- }
- /**
- * 查找质数方法1
- */
- int find_prime( char * array )
- {
- int k , m;
- int count;
- for( k = 2 ; k <= MAX_SIZE ; k++ ){
- for( m =k ;m*k <= MAX_SIZE ; m++)
- *( array + m*k ) = FALSE;
- }
- print_prime( array );
- count = count_number( array );
- return count;
- }
- /**
- * 查找质数方法2
- */
- int find_prime1( char * array )
- {
- int i,j,count;
- for( i = 2 ; i <= sqrt( MAX_SIZE ) ; i++ ){
- if( array[i] == TRUE ){
- for( j = i ; i*j <= MAX_SIZE ; j++ ){
- array[i*j] = FALSE;
- }
- }
- }
- print_prime( array );
- count = count_number( array );
- return count;
- }
- /**
- * 统计质数个数
- */
- int count_number( char * array )
- {
- int k ;
- static int count = 0;
- for(k = 0 ; k <= MAX_SIZE ; k++ ){
- if( *( array + k ) == TRUE )
- count++;
- }
- return count;
- }
- /**
- * 输出质数
- */
- void print_prime( char * array )
- {
- int i;
- for( i = 0 ; i <= MAX_SIZE ; i++ ){
- if( *( array + i ) == TRUE )
- printf("质数:%d\n", i );
- }
- }
vim 7.3
gcc 版本 : 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC)
system : Fedora 14

















