Using the standard mathematical convention where the result is the least nonnegative residue, what is -17 mod 5?
Using the standard mathematical convention where the result is the least nonnegative residue, what is -17 mod 5?
Using the standard mathematical convention where the result is the least nonnegative residue, what is -17 mod 5?
-17 mod 5 = 3.
Under the least-nonnegative-residue convention, a mod n is the unique remainder r satisfying 0 ≤ r < n in the division-algorithm decomposition a = q·n + r, where q is an integer (the quotient).
For a = -17, n = 5:
Check: -17 = 5·(-4) + 3, and 3 lies in {0, 1, 2, 3, 4}, the complete set of least nonnegative residues mod 5.
Why not -2? The value -2 is congruent to -17 mod 5 (they differ by 15 = 3·5), but -2 is negative, so it is not the least nonnegative residue. -2 is what languages using truncated division (e.g., C, Java, Go for %) return for -17 % 5. Languages using floored division (Python's %, or mod in Ruby) return 3, matching the mathematical convention.
Quick verification in Python:
>>> -17 % 5
3
Sources: