It exits on some inputs, just not on the ones you expect, I guess.
I think you wanted unit=2, mod=5 to exit. Look at what happens to the values each time through the loop:
I haven't followed the actual algorithm you're trying to recreate, but line 7 seems suspect to me, since it overwrites the unit. Perhaps instead of line 7 changing unit, you want to calculate x as
I think you wanted unit=2, mod=5 to exit. Look at what happens to the values each time through the loop:
Output:unit=2 mod=5 count=1 x=2
unit=4 mod=5 count=2 x=4
unit=12 mod=5 count=3 x=2
unit=48 mod=5 count=4 x=3
unit=240 mod=5 count=5 x=0
unit=1440 mod=5 count=6 x=0
unit=10080 mod=5 count=7 x=0
unit=80640 mod=5 count=8 x=0
unit=725760 mod=5 count=9 x=0
unit=7257600 mod=5 count=10 x=0
...
x will never become 1 and this loop will never exit.I haven't followed the actual algorithm you're trying to recreate, but line 7 seems suspect to me, since it overwrites the unit. Perhaps instead of line 7 changing unit, you want to calculate x as
x = (unit * count) % mod
?