A typo in an implementation of the Collatz algorithm made it loop infinitely

93 Views Asked by At

This question is kind of related to programming but my question only relates to math in a theoretical sense. I was trying to implement a Collatz conjecture algorithm that calculates the steps it needs for a number to reach $1$.

function:
if x = 1 return;
else
steps = steps + 1
if x is pair:
function(x/2)
if x is odd:
function(3x+1)

Accidentally I wrote in the last line, $3x-1$, which caused an infinite loop where the number after a few recursions becomes it self again. I just want to know what this accidental "conjecture" so to speak is, as to give a name to my bug.

1

There are 1 best solutions below

4
On

This is actually an extension of the Collatz problem to negative inputs, so I would call it that. To see this, exchange $x$ for $-x$ in your calculation. Thus with your typo, $5$ gives

$5,14,7,20,10,5,$

whereas the true Collatz recursion with the input negative yields

$-5,-14,-7,-20,-10,-5.$

As can be seen here, this negative extension of the Collatz problem has multiple cycles; it does not automatically reach $(-)1$.