[Solved] why < is much faster than !=?


When you call cycle with the input value 113383, the process eventually sets n to
827370449, and 3*827370449+1 is 2482111348, which is greater than the maximum signed int and is interpreted as -1812855948. So there’s your first negative number where there should be no negative number.

If this process then eventually sets n to -2, it will loop infinitely between -2 and -1 from then on. There may be other loops I haven’t considered.

If you were to use an unsigned int, there is a possibility (I haven’t checked) that this too will overflow eventually, which will not result in a negative value but will result in an incorrect value, invalidating your results.

No matter what integer representation you use, it would probably be a good idea to compare n with (maximum-1)/3 at the top of each loop, where maximum is the largest possible positive value of your integer type, just to be sure you do not overflow.

solved why < is much faster than !=?