实验6-6 使用函数验证哥德巴赫猜想 (20分)




http://pta.patest.cn/pta/test/13/exam/3/question/478




#include
#include
int prime( int p );
void Goldbach( int n );
int main() {
int m, n, i, cnt;
scanf("%d %d", &m, &n);
if ( prime(m) != 0 ) printf("%d is a prime number\n", m);
if ( m < 6 ) m = 6;
if ( m%2 ) m++;
cnt = 0;
for( i=m; i<=n; i+=2 ) {
Goldbach(i);
cnt++;
if ( cnt%5 ) printf(", ");
else printf("\n");
}
return 0;
}
int prime( int p ) {
int i,count=0;
for(i=2; iif(p%i==0) {
count++;
}
}
if(count==0&&p!=1) {
return 1;
} else {


return 0;
}
}
void Goldbach( int n ) {
int i,k;
if(prime(n) == 0) {
for(i=2; iif(prime(i)!=0) {
k=n-i;
if(prime(k)!=0) {


printf("%d=%d+%d",n,i,k);
break;
}


}
}


}






}

你可能感兴趣的:(C语言)