Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Recipe

Find q and r from the division algorithm

Use when: you have integers a and a positive b and need the unique q, r with a = bq + r and 0 \le r < b

Goal: the exact quotient and remainder pair guaranteed by the division algorithm, even when a is negative

Taught in Divisibility and the division algorithm, revisited

  1. 1

    Compute a/b as a decimal and take the whole number part as a first guess for q.

    Example

    For a=100, b=7: 100/7 \approx 14.28, so the whole-number part gives a first guess of q=14.

    Why: q has to be an integer, and the division algorithm's q is built directly from how many whole copies of b fit into a.

  2. 2

    Subtract bq from a using the guessed q, and check whether the result satisfies 0 \le r < b.

    Example

    For a=-17, b=5: -17/5=-3.4, whole-number part gives q=-3, and r=-17-5(-3)=-17+15=-2, which is negative. Lower q to -4: r=-17-5(-4)=-17+20=3, and now 0 \le 3 < 5.

    • a is zero or positive — the whole-number-part guess from Step 1 already gives a valid r in range, no fix needed.
    • a is negative — the whole-number-part guess can leave r negative, and q must be lowered by 1 (made more negative) to fix it.

    Why: the division algorithm requires 0 \le r < b exactly, and rounding toward zero on a negative a produces a remainder outside that range.

  3. 3

    Check the remainder actually falls between 0 (inclusive) and b (exclusive), 0 \le r < b, before treating it as final.

    Example

    For a=100, b=7: r=100-7(14)=100-98=2, and 0 \le 2 < 7 holds, so q=14, r=2 is the finished answer.

    Why: q and r only count as the division-algorithm pair once this range holds, otherwise it's some other, non-unique split of a.

Common mistakes

  • Rounding q to the nearest integer instead of taking the whole-number part (floor), which can overshoot on negative a.
  • Skipping the re-check of 0 \le r < b after using a calculator's raw decimal quotient.
  • Reporting a negative remainder when a is negative, instead of lowering q by one.
  • Mixing up "b divides a" (r=0) with "a divides b" (b \mid a means r=0 when a is the one being divided).