Collatz Problem Program

Hey guys, I am writing a program that needs to output the amount of times a negative number (1-1000) runs through the collatz conjecture. I have a working program for positive numbers because they always go back to 1 and thats when the program terminates. The problem with negative numbers is that the program terminates when the same number is reached 2 times. With -10 it goes through and reaches -10 again and will terminate. However its not always the same as the first with -41 it reaches -122 twice and will then terminate. This is the program I have right now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

int main ()
{   
    int x[1000], i, count=0, y;
   
    //sets all integers from -1 through -100
    for (i=0; i<1000; i++) {
        
        x[i]=(i+1)*-1;
    }
    
    y=x[i];
    
   for (i=0; i<1000; i++) {
       while (x[i]!= x[i])
       {
           if (x[i] % 2 == 0)
           {
               x[i] = x[i] / 2;
           }
           else
           {
               x[i] = (3 * x[i]+1) / 2;
           }
           count++;
       }
       
       cout<<y<<"         "<<count<<endl;
    
    
    return 0;
}

Any Ideas?? thanks!!
Wait, I just read Collatz on wiki. I thought it terminates when your N reaches 1?

You have this: while (x[i]!= x[i]), which means you terminate when your N finds itself.
thats for all n that are elements of the positive integers. Negative integers can not reach 1 because they will never be positive. So what I need to do now is find a way to make it terminate after it repeats a number that it already outputted. I dont know how to have one number from one array match up to many numbers of a different array. I figured if I could put each output into another array y[i] then check before each time an x[i] is put into y[i] to see if it was already put it; if it was the loop terminates.
Why not use a temporary variable to manipulate and check if that temporary variable is now the same as the original?

how would I check the entire array though? If I have y=x[i] how would I then check the entire array?
Not sure what you mean?

Edit: A better question at this point is: When is it supposed to stop?
Last edited on
From what I know about collatz sequence, loop is supposed to stop when the value you are trying to find it's sequence goes to below 1. So your talk of negative numbers should not matter because you are supposed to terminate whatever loop you are using when the value you are checking is below 1

E: And you only need 2 loops, one to generate the numbers and one to find their collatz sequence.

Hint: Don't check positive numbers because when you think of it, they will will always terminate faster than odd values especially the larger they are
Last edited on
So what I need to do now is find a way to make it terminate after it repeats a number that it already outputted.


You can do this with a set<int>.
Topic archived. No new replies allowed.