|
判定素数的,水题,主要是练习一下筛选法。
#include <stdio.h> #include <math.h> #define DEBUG 1 short a[1000000] ; int main() { #if DEBUG freopen("C:\\Documents and Settings\\Administrator\\桌面\\in.txt","r",stdin); freopen("C:\\Documents and Settings\\Administrator\\桌面\\out.txt","w",stdout); #endif int i, j, n, flag ; double temp ; for( i=2; i<1000000; ++i ){ if( a[i] ) continue ; for( j=i; j<1000000; j+=i ) a[j] = 1 ; temp = sqrt( i ) ; flag = 1 ; for( j=2; j<=temp; ++j ){ if( i%j == 0 ){ flag = 0 ; break ; } } if( flag ) a[i] = 0 ; } while( scanf("%d", &n ) && n ){ for( i=3; i<n; i+=2 ){ if( !a[i] && !a[n-i] ){ printf("%d = %d + %d\n", n, i, n-i ) ; break ; } } } return 0 ; }
|