#include<stdio.h>
#include<math.h>
#include<time.h>
int isPrime(long int i);//判断是否为素数
int isGold(long int i);//判断是否符合哥德巴赫猜想
int main()
{
long int low,hig,temp;
clock_t start, finish;
double duration;
star: setbuf(stdin,NULL);
printf("please putinto low:");//输入范围
scanf("%ld",&low);
printf("please putinto hig:");
scanf("%ld",&hig);
if(low % 2 == 0)//判断是否符合要求就不加非数字判断了啊
{
if(low>=hig)
{
printf("low can not bigger than hig!\n");
goto star;
}
else
{
if(low <= 2)
{
printf("low can not smaller than 2!\n");
goto star;
}
else
{
if(hig %2 ==0)
{
}
else
{
printf("hig必须是偶数!\n");
goto star;
}
}
}
}
else
{
printf("low必须是偶数!\n");
goto star;
}
start = clock();//计算判断所需时间
for(temp = low;temp <= hig;temp=temp+2)//判断范围内数字是否符合
{
if(isGold(temp) == 0)
{
break;
}
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
if(temp == hig)
{
printf("在%ld~%ld符合哥德巴赫猜想\n",low,hig);
printf("验证过程所用时间为%lf seconds\n", duration);
goto star;
}
else
{
printf("在%ld~%ld不符合哥德巴赫猜想\n",low,hig);
printf("验证过程所用时间为%lf seconds\n", duration);
goto star;
}
return 0;
}
int isPrime(long int i)
{
long int j;
if(i == 2)
{
return 1;
}
if(i%2==0)
{
return 0;
}
for(j = 3;j<= sqrt(i);j = j + 2)
{
if(i%j == 0)
{
return 0;
}
}
return 1;
}
int isGold(long int i)
{
int j;
j = i - 2;
if(isPrime(j)==1)
{
return 1;
}
for(j = 3;j <= i/2;j = j + 2)
{
if(isPrime(i-j)==1)
{
return 1;
}
}
return 0;
}
|