Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2538773
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2011-03-30 09:02:03

    哥德巴赫猜想是说任何一个大于2的偶数都能表示成为两个素数之和。
    一个正偶数a可以表示成为a/2种正整数相加的形式:
  1. #include <stdio.h>
  2. #include <string.h>

  3. int isGoldbach(int a);
  4. int testGBGuess(int low, int high);
  5. int isPrime(int i);

  6. int main(int argc, char *argv[])
  7. {
  8.   printf("now testify goldbach Guess in the rang of 1~240\n\n");
  9.   if(testGBGuess(1,240))
  10.     printf("\nin the range of 1~240,Goldbach guess is right.\n");
  11.   else
  12.     printf("\nGoldbach Guess is wrong.\n");
  13.   
  14.   return 0;
  15. }

  16. int isGoldbach(int a)
  17. {
  18.   int i,flag = 0;
  19.   for(i=1; i<=a/2; i++)
  20.   {
  21.     if(isPrime(i) && isPrime(a-i))
  22.     {
  23.       flag = 1;
  24.       printf("%d = %d + %d ",a,i,a-i);
  25.       break;
  26.     }
  27.   }

  28.   if(flag == 1)
  29.     return 1;
  30.   else
  31.     return 0;
  32. }

  33. int isPrime(int i)
  34. {
  35.   int n,flag = 1;
  36.   if(1 == i)
  37.     return 0;

  38.   for(n=2; n<i; n++)
  39.     if(i % n == 0)
  40.     {
  41.       flag = 0;
  42.       break;
  43.     }

  44.   if(flag == 1)
  45.     return 1;
  46.   else
  47.     return 0;
  48. }

  49. int testGBGuess(int low, int high)
  50. {
  51.   int i,j = 0;
  52.   int flag = 0;
  53.   for(i=low; i<=high; i++)
  54.   {
  55.     if(i %2 == 0 && i > 2)
  56.     {
  57.       if(isGoldbach(i))
  58.       {
  59.     j++;
  60.         if(j == 5)
  61.     {
  62.      printf("\n");
  63.           j = 0;
  64.         }
  65.       }
  66.       else
  67.       {
  68.         flag = 1;
  69.         break;
  70.       }
  71.     }
  72.   }
  73.   
  74.   if(flag == 0)
  75.     return 1;
  76.   else
  77.     return 0;
  78. }
peng@ubuntu:/media/XIAOPENG_U$ ./a.out 
now testify goldbach Guess in the rang of 1~240

4 = 2 + 2 6 = 3 + 3 8 = 3 + 5 10 = 3 + 7 12 = 5 + 7
14 = 3 + 11 16 = 3 + 13 18 = 5 + 13 20 = 3 + 17 22 = 3 + 19
24 = 5 + 19 26 = 3 + 23 28 = 5 + 23 30 = 7 + 23 32 = 3 + 29
34 = 3 + 31 36 = 5 + 31 38 = 7 + 31 40 = 3 + 37 42 = 5 + 37
44 = 3 + 41 46 = 3 + 43 48 = 5 + 43 50 = 3 + 47 52 = 5 + 47
54 = 7 + 47 56 = 3 + 53 58 = 5 + 53 60 = 7 + 53 62 = 3 + 59
64 = 3 + 61 66 = 5 + 61 68 = 7 + 61 70 = 3 + 67 72 = 5 + 67
74 = 3 + 71 76 = 3 + 73 78 = 5 + 73 80 = 7 + 73 82 = 3 + 79
84 = 5 + 79 86 = 3 + 83 88 = 5 + 83 90 = 7 + 83 92 = 3 + 89
94 = 5 + 89 96 = 7 + 89 98 = 19 + 79 100 = 3 + 97 102 = 5 + 97
104 = 3 + 101 106 = 3 + 103 108 = 5 + 103 110 = 3 + 107 112 = 3 + 109
114 = 5 + 109 116 = 3 + 113 118 = 5 + 113 120 = 7 + 113 122 = 13 + 109
124 = 11 + 113 126 = 13 + 113 128 = 19 + 109 130 = 3 + 127 132 = 5 + 127
134 = 3 + 131 136 = 5 + 131 138 = 7 + 131 140 = 3 + 137 142 = 3 + 139
144 = 5 + 139 146 = 7 + 139 148 = 11 + 137 150 = 11 + 139 152 = 3 + 149
154 = 3 + 151 156 = 5 + 151 158 = 7 + 151 160 = 3 + 157 162 = 5 + 157
164 = 7 + 157 166 = 3 + 163 168 = 5 + 163 170 = 3 + 167 172 = 5 + 167
174 = 7 + 167 176 = 3 + 173 178 = 5 + 173 180 = 7 + 173 182 = 3 + 179
184 = 3 + 181 186 = 5 + 181 188 = 7 + 181 190 = 11 + 179 192 = 11 + 181
194 = 3 + 191 196 = 3 + 193 198 = 5 + 193 200 = 3 + 197 202 = 3 + 199
204 = 5 + 199 206 = 7 + 199 208 = 11 + 197 210 = 11 + 199 212 = 13 + 199
214 = 3 + 211 216 = 5 + 211 218 = 7 + 211 220 = 23 + 197 222 = 11 + 211
224 = 13 + 211 226 = 3 + 223 228 = 5 + 223 230 = 3 + 227 232 = 3 + 229
234 = 5 + 229 236 = 3 + 233 238 = 5 + 233 240 = 7 + 233
in the range of 1~240,Goldbach guess is right.


阅读(1078) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~