For the number 113383
, iteration #120 yields 827370449
. The next iteration numerically yields 2482111348
which is too large to fit in an int
variable, so it causes an arithmetic overflow that wraps around to a negative number.
From there, all iterations result in a negative number, and although the result repeatedy cycles through -1
, the loop termination condition of the result being 1
never occurs, so an infinite loop results.
However, if you change the variable type to long
, you avoid the overflow (for this starting number anyway), and the sequence finishes after 247 iterations.
Incidentally, I found this out by simply printing each iteration and it became immediately obvious what had happened. You could have done the same thing and solved this yourself. Debugging is as handy a skill as coding, because we all hit coding situations that surprise us and finding the reason yourself is always more satisfying and memorable (so you don't fall for the same trap again).