C 验证哥德巴赫猜想

#include<math.h>
int checkPrimer(double a){       //验证是否为质数

    unsigned long i, asqrt;
    asqrt = (unsigned long)sqrt(a);
    for(i =2;i<= asqrt;i++){
       if((int)a % i == 0)
           return 0;
    }
    return 1;

}

int checkTruth(unsigned long a, unsigned long *NumberA , unsigned long *NumberB){

    unsigned long i, half  = a/2;
    for(i=3; i<half; i+=2){
        if(checkPrimer((double)i) && checkPrimer((double)(a-i))){
             *NumberA = i;
             *NumberB = a-i;
             return 1;
        }
    }
    return 0;

}

test.h 文件


#include <stdio.h>
#include<math.h>
#include "test1.h"

void main(){
    
    unsigned long a ,p1,p2;
    do{
       printf("请输入不小于6的偶数\n");
       scanf("%lu",&a);
       if( a>=6 && a%2 ==0 ){
           if(checkTruth(a,&p1,&p2)){
               printf("OK , %d   %d\n",p1,p2);
           }else{
               printf("sorry!");
           }
       }
     
    }while(a !=0);
    return ;
}
 

test.c 文件

判断一个大于6的偶数都可以是由两个质数组成

运行结果:

posted @ 2012-10-11 01:25  ﹏Sakura  阅读(460)  评论(0编辑  收藏  举报