• + 0 comments

    So essentially we're dealing with a finite integer field of 2^31. So the key is to first test whether S, Q or P are coprime with 2^31. If any of them are coprime that means the sequence will NOT repeat. Thus the result will be N.

    Only go on after that check with the obvious algorithm of calculating the sequence and seeing when the sequence repeats. And you can stop as soon as you encounter a previously appearing number.