|
发表于 2014-1-27 12:38:04
|
显示全部楼层
曾拥有的方向 发表于 2014-1-27 12:00
恩恩 谢谢啦 ,
最后判断这个范围内的数字是否符合猜想的时候写错了- #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;
- int k;
- clock_t start, finish;
- double duration;
- star: setbuf(stdin,NULL);
- k = 1;
- 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)
- {
- k = 0;
- break;
- }
- }
- finish = clock();
- duration = (double)(finish - start) / CLOCKS_PER_SEC;
- if(k == 1)
- {
- 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+1;j = j + 2)
- {
- if(isPrime(i-j)==1 && isPrime(j)==1)
- {
- return 1;
- }
- }
-
- return 0;
- }
复制代码 OK了
至于卡住了是因为要循环的次数太多 我估计你开着去吃顿饭回来应该有结果了 |
|