数学趣题——验证角谷猜想

1、题目

任意给定一个自然数,若它为偶数则除以2,若它为奇数则乘以3,得到一个新的自然数,照这样计算下去,若干次后必然得到1。编程验证

2、源码

   1: #include <stdio.h>
   2:  
   3: void proveJiaoGu(int n)
   4: {
   5:     int count = 1;
   6:  
   7:     while(n != 1 && count <= 1000) /*阈值设为1000*/
   8:     {
   9:         if(n % 2 == 0) /*n为偶数*/
  10:         {
  11:             printf("%d/2=%d\n", n, n / 2);
  12:             n = n / 2;
  13:         }
  14:         else
  15:         {
  16:             printf("%d*3+1=%d\n", n, n * 3 + 1); /*n为奇数*/
  17:             n = n * 3 + 1;
  18:         }
  19:  
  20:         count++;
  21:     }
  22:  
  23:     if(count < 1000 && n == 1)
  24:         printf("This natural number  is according to JiaoGu Guess\n");
  25: }
  26:  
  27: int main()
  28: {
  29:     int n;
  30:     printf("Please input a number to verify\n");
  31:     scanf("%d", &n);
  32:     printf("-------- Step of Verification---------\n");
  33:     proveJiaoGu(n);
  34:     return 0;
  35: }

你可能感兴趣的:(验证)