数学趣题——哥德巴赫猜想的近似证明

一、分析

    可在较小范围内使用枚举法,验证每一个偶数是否能表示成为两个素数的和。

二、源码

   1: #include <string.h>
   2: #include <stdio.h>
   3:  
   4: int IsGoldbach(int a);
   5: int TestifyGB_Guess(int low, int high);
   6: int IsPrime(int i);
   7:  
   8: int main()
   9: {
  10:     printf("Test 1-100 is Goldbach Guess\n");
  11:     if (TestifyGB_Guess(1, 100))
  12:         printf("in 1-100 guess is right\n");
  13:     else
  14:         printf("guess is wrong\n");
  15:     return 0;
  16: }
  17:  
  18: int TestifyGB_Guess(int low, int high)
  19: {
  20:     int i, j = 0;
  21:     int flag = 0;
  22:  
  23:     for (i = low; i <= high; ++i)
  24:     {
  25:         if (i % 2 == 0 && i > 2)
  26:         {
  27:             if (IsGoldbach(i))
  28:             {
  29:                 j++;
  30:                 if (j == 5)
  31:                 {
  32:                     printf("\n");
  33:                     j = 0;
  34:                 }
  35:             }
  36:             else
  37:             {
  38:                 flag = 1;
  39:                 break;
  40:             }
  41:         }
  42:     }
  43:  
  44:     if (flag == 0)
  45:         return 1;
  46:     else
  47:         return 0;
  48: }
  49:  
  50: int IsGoldbach(int a)
  51: {
  52:     int i, flag = 0;
  53:     for (i = 1; i <= a / 2; ++i)
  54:     {
  55:         if (IsPrime(i) && IsPrime(a - i))
  56:         {
  57:             flag = 1;
  58:             printf("%d=%d+%d ", a, i, a-i);
  59:             break;
  60:         }
  61:     }
  62:  
  63:     if (flag == 1)
  64:         return 1;
  65:     else
  66:         return 0;
  67: }
  68:  
  69: int IsPrime(int i)
  70: {
  71:     int n, flag = 1;
  72:     if (1 == i)
  73:         flag = 0;
  74:  
  75:     for (n=2; n<i; n++)
  76:     {
  77:         if (i%n == 0)
  78:         {
  79:             flag = 0;
  80:             break;
  81:         }
  82:     }
  83:  
  84:     if (flag == 1)
  85:         return 1;
  86:     else
  87:         return 0;
  88: }
  89:  
posted @ 2010-05-25 09:27  红脸书生  阅读(449)  评论(0编辑  收藏  举报