Collatz Conjecture Meet of 2 Numbers I need the most efficient way to calculate when the Collatz Conjecture of two numbers, a and b respectively, are the same. Here is my algorithm, which is too slow:

while(true) {
        int a = sc.nextInt();
        int b = sc.nextInt();

        if(a == 0 &amp;&amp; b == 0) {
            break;
        }

        TreeMap&lt;Integer, Integer&gt; visited = new TreeMap&lt;&gt;();
        visited.put(a, 0);

        int A = a;
        int Acount = 0;

        while(A != 1) {
            Acount++;

            if(A % 2 == 0) {
                A /= 2;
            } else {
                A = A * 3 + 1;
            }

            if(!visited.containsKey(A)) {
                visited.put(A, Acount);
            }
        }

        int B = b;
        int Bcount = 0;

        while(B != 1) {
            if(visited.containsKey(B)) {
                out.println(a + " needs " + visited.get(B) + " steps, " + b + " needs " + Bcount + " steps, they meet at " + B);
                break;
            }

            if(B % 2 == 0) {
                B /= 2;
            } else {
                B = B * 3 + 1;
            }

            Bcount++;
        }

    }


After looking around on Github, I found this, and it worked, although it's essentially the exact same thing.

Original problem

How do I fix my Java code to make it more efficient? I tried it literally at least four times.
Integer Overflow

I suspect that your problem has to do with integer overflow and not with your algorithm itself.  If you don't guard against integer overflow, doing A = A * 3 + 1 could result in a negative value, and you may end up in an infinite loop where you never reach 1.  Try changing your variables to long and resubmit.

By the way, to test for infinite loop cases, try the starting value 113383.

Other suggestions


You could use a HashMap instead of a TreeMap.  A HashMap has \$O(1)\$ insert and lookup compared to \$O(\log n)\$ for a TreeMap.  The difference for this program will be very small though.
In the A loop, you shouldn't have to check if visited.containsKey(A) because the collatz sequence never repeats until you reach 1.  You can just always add A to the map.
If b is 1, your program never prints anything.