鱼C论坛

 找回密码
 立即注册
查看: 1997|回复: 4

大家能帮我看看这个哥德巴赫猜想到底哪里出错了么,万分感谢

[复制链接]
发表于 2014-12-31 09:32:41 | 显示全部楼层 |阅读模式
1鱼币
#include<stdio.h>//验证哥德巴赫猜想:任意一个大于6的偶数都是由两个素数组成的
void main()
{
        int a,n,flat,flatt,k,i;//a是要验证的那个偶数,n是第一个素数,flat flatt是标志,k、i起扫描作用
        flat=2,flatt=2;
        printf("please input\n");
        scanf("%d",&a);
        for(n=3;n<=(a/2),flat!=1;n++)//先从3开始验证是否是素数
        {
                for(i=2;i<n,flat!=0;i++)
                {
                        if(0==n%i)
                                flat=0;//n不是素数,flat=0,for循环结束
                        else
                                flat=1;//还不能判断是不是素数,因此继续for循环
                       
                }
               
                for(k=2;k<(a-n),flatt!=0,flat==1;k++)//flat==1,说明n是素数,for循环启动
                {
                        if(0==(a-n)%k)//(a-n)不是素数,flat=0,for循环结束
                                flatt=0;
                        else
                                flatt=1;//还不能判断是不是素数,因此继续for循环       
                       
                }
               
        }
       
        if((1==flat)&&(1==flatt))//如果flat、flatt都为1说明n和(a-n)都是素数,输出"fuhe"以及n和(a-n)
                printf("fuhe %d  %d\n",n,(a-n));
        else
                printf("bu fuhe\n");
}


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-12-31 14:20:05 | 显示全部楼层
第一,for循环中的判断条件想要同时成立用的是&&而不是逗号
第二,大的for循环的判断条件不对, for(n=3;n<=(a/2),flat!=1;n++)
n从3开始执行,3是素数,flat=1;大的循环直接跳出;
所以无论你输入什么出来的结果都是 4,a-4
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-12-31 20:07:31 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<time.h>

  4. int isPrime(long int i);//判断是否为素数
  5. int isGold(long int i);//判断是否符合哥德巴赫猜想

  6. int main()
  7. {
  8.                 long int low,hig,temp;
  9.                 clock_t start, finish;
  10.                 double  duration;
  11. star:        setbuf(stdin,NULL);
  12.                 printf("please putinto low:");//输入范围
  13.                 scanf("%ld",&low);
  14.                 printf("please putinto hig:");
  15.                 scanf("%ld",&hig);
  16.                
  17.                 if(low % 2 == 0)//判断是否符合要求就不加非数字判断了啊
  18.                 {
  19.                         if(low>=hig)
  20.                         {
  21.                                 printf("low can not bigger than hig!\n");
  22.                                 goto star;
  23.                         }
  24.                         else
  25.                         {
  26.                                 if(low <= 2)
  27.                                 {
  28.                                         printf("low can not smaller than 2!\n");
  29.                                         goto star;
  30.                                 }
  31.                                 else
  32.                                 {
  33.                                         if(hig %2 ==0)
  34.                                         {
  35.                                         }
  36.                                         else
  37.                                         {
  38.                                                 printf("hig必须是偶数!\n");
  39.                                                 goto star;
  40.                                         }
  41.                                 }
  42.                         }
  43.                 }
  44.                 else
  45.                 {
  46.                         printf("low必须是偶数!\n");
  47.                         goto star;
  48.                 }
  49.                 start = clock();//计算判断所需时间
  50.                 for(temp = low;temp <= hig;temp=temp+2)//判断范围内数字是否符合
  51.                 {
  52.                         if(isGold(temp) == 0)
  53.                         {
  54.                                 break;
  55.                         }
  56.                 }
  57.                 finish = clock();
  58.                 duration = (double)(finish - start) / CLOCKS_PER_SEC;
  59.                 if(temp == hig)
  60.                 {
  61.                         printf("在%ld~%ld符合哥德巴赫猜想\n",low,hig);
  62.                         printf("验证过程所用时间为%lf seconds\n", duration);
  63.                         goto star;
  64.                 }
  65.                 else
  66.                 {
  67.                         printf("在%ld~%ld不符合哥德巴赫猜想\n",low,hig);
  68.                         printf("验证过程所用时间为%lf seconds\n", duration);
  69.                         goto star;
  70.                 }
  71.                
  72.                 return 0;
  73. }

  74. int isPrime(long int i)
  75. {
  76.         long int j;
  77.         if(i == 2)
  78.         {
  79.                 return 1;
  80.         }
  81.         if(i%2==0)
  82.         {
  83.                 return 0;
  84.         }
  85.         for(j = 3;j<= sqrt(i);j = j + 2)
  86.         {
  87.                 if(i%j == 0)
  88.                 {
  89.                         return 0;
  90.                 }
  91.         }
  92.         return 1;
  93. }

  94. int isGold(long int i)
  95. {
  96.         int j;
  97.         j = i - 2;
  98.         if(isPrime(j)==1)
  99.         {
  100.                 return 1;
  101.         }
  102.         for(j = 3;j <= i/2;j = j + 2)
  103.         {
  104.                 if(isPrime(i-j)==1)
  105.                 {
  106.                         return 1;
  107.                 }
  108.         }
  109.        
  110.         return 0;
  111. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-1-1 09:47:19 | 显示全部楼层
猴子给我AK 发表于 2014-12-31 14:20
第一,for循环中的判断条件想要同时成立用的是&&而不是逗号
第二,大的for循环的判断条件不对, for(n=3;n

谢谢您的回复!
关于您提的第二条
flat==1的时候还有第二个for循环语句了呀
为什么会跳出到大的for循环呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-1-1 16:43:40 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 17:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表