【问题标题】:C program stops on certain number [duplicate]C程序在某个数字处停止[重复]
【发布时间】:2021-09-09 14:21:56
【问题描述】:

您好,我是 C 新手,我选择了一个项目来变得更好
如果根据 Collat​​z 猜想,我想制作一个程序来暴力破解每个数字。

科拉茨猜想

对于那些不知道 Collat​​z 猜想是什么的人,请点击 here
总结
如果一个数是奇数,则乘以 3 并增加 1
如果它被2除
重复此操作,直到达到 1

我的代码:

#include <stdio.h>
int main(){
    int n, x, a, b;
    n = 5; //number we check first (has to be bigger than 4 because 4,3,2,1 is a loop)
    //n is later on a number that is currently being tested
    //all numbers smaller than 2 ** 64 were brute force tested and are according to Collatz's conjecture
    x = n; //x begins as n and then changes according to rules of cenjecture until it reaches 1
    a = 1; //a is set to 1 until x = 1 which means that number n is according to conjecture
    b = 1; //creates an infinite loop

    while(b == 1){ //runs forever
        if(a == 1){
            if(x == 1){
                a = 0;              //if x reaches 1 (number n is according to conjecture) a is set to 0 and n is increased by 1 ===
            }                       //                                                                                            ||
            else{                   //                                                                                            ||
                if(x % 2 == 0){     //                                                                                            ||
                    x = x / 2;      //                                                                                            ||
                }                   //                                                                                            ||
                else{               //                                                                                            ||
                    x = x * 3 + 1;  //                                                                                            ||
                }                   //                                                                                            ||                  
            }                       //                                                                                            ||
        }                           //                                                                                            ||
        else{                       //                                                                                            ||
            printf("%d \n", n);     //                                                                                            ||
            n = n + 1;              //             <<<<<============================================================================
            x = n;                  //
            a = 1;                  //
        }
    }
}

问题

问题是,当我运行它时,它会在编号 113383 处停止并且出现问题。我什至让它运行了 5 分钟以上,但它什么也没做。我什至尝试在我的 python 程序中运行相同的数字来测试输入数字,并且它很快就有了。 我尝试从 n = 113384 开始,它工作并在 134378 再次停止。

数字 113383 在 C 中是否有所不同,或者我的代码中是否存在缺陷。

如果可以,请提供帮助。
非常感谢

【问题讨论】:

  • 如果你想要一个无限循环,你可以写while (1),你不需要额外的变量b

标签: c gcc collatz


【解决方案1】:

尝试将变量声明为“long int”,这可能是因为 int 女巫的最大大小为 –32,767 到 32,767

【讨论】:

  • int 通常是 32 位。你的范围是short
  • 实际上从113383开始的数列达到最大值2482111348,对于带符号的32位int来说太大了
  • int的上限为2,147,483,647
  • 标准指定的int最小范围是 –32,767 到 32,767,但大多数现代实现的范围是 -2147483648 到 2147483647。
  • 这确实是正确的假设:collatz(113382) 中的数字不能再用 31 位表示是在 827370449 之后的一位,因为 (827370449 * 3 + 1) - 2^31 = 334627700。在大多数情况下,这将导致一个负数(但它仍然是 UB!),此处为 -1812855948。您可以使用更大的整数类型,但至少要检查溢出。
猜你喜欢
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 2010-12-18
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 2016-05-22
相关资源
最近更新 更多