Mod in Java produces negative numbers
The problem here is that in Python the % operator returns the modulus and in Java it returns the remainder. These functions give the same values for positive arguments, but the modulus always returns positive results for negative input, whereas the remainder may give negative results.
What’s the difference between “mod” and “remainder”?
-21 mod 4 is 3 because -21 + 4 x 6 is 3.
But -21 divided by 4 gives -5 with a remainder of -1.