083.验证歌德巴赫猜想

举报
C语言与CPP编程 发表于 2022/05/01 22:30:29 2022/05/01
【摘要】 #include<stdio.h>#include<math.h>int fflag(int n);void main(){ int i,j,n; long max; clrscr(); puts("=============================================...

  
  1. #include<stdio.h>
  2. #include<math.h>
  3. int fflag(int n);
  4. void main()
  5. {
  6. int i,j,n;
  7. long max;
  8. clrscr();
  9. puts("============================================================");
  10. puts("|| This program will verify the Goldbach Guess. ||");
  11. puts("|| That is any positive even number can be broken up into ||");
  12. puts("|| the sum of two prime numbers. ||");
  13. puts("|| e.g., 4=2+2, 6=3+3, 8=3+5, 10=3+7, 12=5+7,... ||");
  14. puts("============================================================");
  15. printf("\n >> Please input the scale n you want to verify : ");
  16. scanf("%ld",&max);
  17. printf("\n >> Now the program starts to verify the even number\n");
  18. printf(" >> less than %ld equals to sum of two prime numbers.\n\n",max);
  19. for(i=4,j=0;i<=max;i+=2)
  20. {
  21. for(n=2;n<i;n++) /*将偶数i分解为两个整数*/
  22. if(fflag(n)) /*分别判断两个整数是否均为素数*/
  23. if(fflag(i-n))
  24. {
  25. printf("%4d=%2d+%2d ",i,n,i-n); /*若均是素数则输出*/
  26. j++;
  27. if(j==5)
  28. {
  29. printf("\n");
  30. j=0;
  31. }
  32. break;
  33. }
  34. if(n==i) printf("error %d\n",i);
  35. }
  36. puts("\n >> Press any key to quit...");
  37. getch();
  38. }
  39. int fflag(int i) /*判断是否为素数*/
  40. {
  41. int j;
  42. if(i<=1)return 0;
  43. if(i==2)return 1;
  44. if(!(i%2))return 0; /*if no,return 0*/
  45. for(j=3;j<=(int)(sqrt((double)i)+1);j+=2)
  46. if(!(i%j))return 0;
  47. return 1; /*if yes,return 1*/
  48. }

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124518658

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。