Modular arithmetic

Use this for cyclic patterns, wrapping indices, and nonnegative remainders.

0/5 marked as readthis browser

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.

KEEP THIS RELATIONSHIP

For a positive modulus m, write n = qm + r with 0 ≤ r < m. This r is the nonnegative remainder.

A worked example.

−17 mod 5
  1. 1

    −17 = (−4) × 5 + 3.

  2. 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.

Question 1 of 30 correct / 0 answered

Modular arithmetic

What is the nonnegative remainder of −8 modulo 5?

Videos and further reading

These resources open in a new tab on the publisher’s website.

Finished reading?Stored in this browser; separate from practice grades.

Put the idea to work.

Try a generated question and get a worked explanation after checking your answer.

Go to practice