What’s happening here?
Modular arithmetic groups integers that differ by multiples of a fixed positive modulus. You can picture wrapping around a clock. A negative input still has a unique remainder in the range 0 through m − 1 under the convention used here.
For a positive modulus m, write n = qm + r with 0 ≤ r < m. This r is the nonnegative remainder.
A worked example.
- 1
−17 = (−4) × 5 + 3.
- 2
The remainder is 3, so −17 and 3 are congruent modulo 5.
A common wrong turn.
Programming languages can use a signed remainder for negative inputs. These questions explicitly ask for the nonnegative representative.
Why this works.
Modular arithmetic groups integers that differ by a multiple of m. Every group has exactly one representative in 0 through m−1. A programming language’s remainder operator may return a negative value, so check the convention when working with negative inputs.
Check it. Apply it. Explain it.
Three questions, with hints and worked feedback. Your score counts your first submitted answer. Practice only · scores stay in this session and do not enter your account history.
Modular arithmetic
Videos and further reading
Lecture 9: Modular arithmetic
ocw.mit.edu · New tabModular arithmetic
en.wikipedia.org · New tabThese resources open in a new tab on the publisher’s website.
Finished reading?Stored in this browser; separate from practice grades.