你好,我是C语言的新手,我选择了一个项目来做得更好
我想做一个程序,如果它是根据Collatz猜想,那么它会暴力破解每个数字。
Collatz猜想:
对于那些不知道Collatz猜想是什么的人点击这里
摘要:
如果一个数字是奇数,它乘以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中是不同的,还是我的代码中有缺陷。
请帮助如果你可以。
非常感谢。
尝试将变量声明为"long into "这可能是由于int类型的最大大小为-32,767到32,767