2

So, I was just trying to create a loop to run the "3n+1" formula and when I enter a negative number I get stuck in an infinite loop with a remainder of 0 and -1.

Is that correct or is my code missing something?

Here is my code:

    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");
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75

2 Answers2

4

This conjecture holds only for natural numbers (i.e. positive integers 1, 2, 3,...). If you want to extend it to 0 and negative numbers, you will have to use some other formula. Check out "Extensions to larger domains" on https://en.wikipedia.org/wiki/Collatz_conjecture.

Nikhil B
  • 76
  • 3
0

Several loops get formed. Further googling will show you there are 3-4 (google for an exact count) loops formed. And then the rest of the negative numbers should also get back down to $-1$ or in other words "something analogous" to the positive case. So you got stuck in the first loop! LOL

I think it should be considered extended to the group of integers so that there is a hope of immediately applying Group Cohomology techniques instead of Monoid Cohomology techniques (which are less explored).

MathCrackExchange
  • 595
  • 1
  • 6
  • 25