我的“Collat​​z猜想”有什么问题?

时间:2016-01-10 04:41:09

标签: java collatz

所以,我只是想创建一个循环来运行“3n + 1”公式,当我输入一个负数时,我会陷入一个余数为0和-1的无限循环中。

这是正确的还是我的代码丢失了什么?

这是我的代码:

    Scanner scan = new Scanner(System.in);
    number = 0;
    method = 0;
int counter= 0;

if(scan.hasNextInt()){
     number = scan.nextInt();
  int original = number;
 while(number!=1){
      method = number%2;
     if(method==0){
    number = number/2;
 }else number = number*3+1;
 counter +=1;
  System.out.println(number);
  System.out.println("the remainder was "+method);
 }


 System.out.println("The original number was "+original);
 System.out.println("it took " + counter+ " times to reach 1.");



}else System.out.println("please enter a number");

1 个答案:

答案 0 :(得分:2)

这个猜想仅适用于自然数(即正整数1,2,3,......)。如果要将其扩展为0和负数,则必须使用其他一些公式。查看"扩展到更大的域名"在https://en.wikipedia.org/wiki/Collatz_conjecture