The division algorithm
Statement
Let \( a \in \mathbb{Z} \) and let \( b \in \mathbb{Z} \) with \( b \neq 0 \). Then there exist integers \( q \) and \( r \), and only one such pair, satisfying \[ a = bq + r \qquad\text{and}\qquad 0 \le r \lt |b| . \] The integer \( q \) is called the quotient and \( r \) the remainder of \( a \) on division by \( b \). Explicitly \( q = \lfloor a/b \rfloor \) when \( b \gt 0 \) and \( q = \lceil a/b \rceil \) when \( b \lt 0 \), and in both cases \( r = a - bq \). The name is traditional but misleading: the assertion is a theorem of existence and uniqueness about \( \mathbb{Z} \), not an algorithm, and its proof rests only on the well-ordering of \( \mathbb{Z}_{\ge 0} \).
Why it matters
Almost every construction in elementary number theory is this theorem applied repeatedly. The Euclidean algorithm is nothing but the division algorithm iterated on \( (b, r) \) until the remainder vanishes, and its termination is exactly the strict decrease \( |b| \gt r \ge 0 \) guaranteed here; from that follow Bézout's identity, Euclid's lemma, the Fundamental Theorem of Arithmetic, and the computability of modular inverses. Modular arithmetic itself is a corollary: the theorem is precisely the assertion that the \( n \) residue classes \( \overline{0}, \overline{1}, \ldots, \overline{n-1} \) exhaust \( \mathbb{Z}/n\mathbb{Z} \) without repetition, so that \( |\mathbb{Z}/n\mathbb{Z}| = n \). Positional notation — base \( 10 \), base \( 2 \), base \( 16 \) — is the same theorem applied recursively to successive quotients, which is why every computer's integer arithmetic is built on it.
Structurally, the theorem is the definition of \( \mathbb{Z} \) as a Euclidean domain: an integral domain carrying a size function (here \( n \mapsto |n| \)) with respect to which remainders can always be made strictly smaller than the divisor. Every consequence of that property — that \( \mathbb{Z} \) is a principal ideal domain, hence a unique factorisation domain — is downstream of this one paragraph. The identical statement with \( |n| \) replaced by \( \deg \) holds in \( F[t] \) for a field \( F \), and that single substitution transports the whole of elementary number theory into polynomial algebra: gcds of polynomials, partial fractions, the structure of \( F[t]/(f) \), and the classification of finite fields.
Hypotheses
Proof
We prove existence first for \( b \gt 0 \), then uniqueness in the same range, then reduce the case \( b \lt 0 \) to it by a sign change. Throughout, \( q \) and \( r \) denote integers and every inequality is an inequality of integers, so that “strictly smaller non-negative integer” carries its full force.
Result
Reading. Quotient and remainder are not chosen, they are forced. Given the dividend, the divisor and the convention \( 0 \le r \lt |b| \), there is exactly one way to split \( a \) into “a whole number of copies of \( b \)” plus “what is left over”, and the leftover is always non-negative and always strictly smaller in size than the divisor.
Scope. All \( a \in \mathbb{Z} \), including negative \( a \), and all \( b \neq 0 \), including negative \( b \). The remainder never inherits the sign of \( a \) or of \( b \). The statement is false in every field, meaningless for \( b = 0 \), and generalises verbatim to any Euclidean domain once \( |\cdot| \) is replaced by that domain's Euclidean function — \( \deg \) in \( F[t] \), the norm \( N(x+iy) = x^2+y^2 \) in \( \mathbb{Z}[i] \) — though uniqueness of \( (q,r) \) is special to \( \mathbb{Z} \) and \( F[t] \) and fails in \( \mathbb{Z}[i] \).
Corollaries & converses
- Divisibility test. \( b \mid a \) if and only if \( r = 0 \). (If \( r = 0 \) then \( a = bq \); conversely if \( a = bc \) then \( (c, 0) \) is an admissible pair, and uniqueness forces \( r = 0 \).) This turns a quantified statement, “\( \exists c: a = bc \)”, into a finite computation.
- Residue classes. For \( n \ge 1 \) every integer is congruent modulo \( n \) to exactly one of \( 0, 1, \ldots, n-1 \), so \( \mathbb{Z}/n\mathbb{Z} \) has exactly \( n \) elements and \( \{0,\ldots,n-1\} \) is a complete residue system. Existence gives “at least one”, uniqueness gives “at most one”.
- The Euclidean algorithm. \( \gcd(a,b) = \gcd(b,r) \) whenever \( a = bq+r \), because the pairs \( (a,b) \) and \( (b,r) \) have identical sets of common divisors. Iterating produces a strictly decreasing sequence of non-negative remainders, which must terminate; the last non-zero remainder is \( \gcd(a,b) \). See the Euclidean algorithm and Bézout's identity.
- Base-\( \beta \) representation. For \( \beta \ge 2 \), every \( n \in \mathbb{Z}_{\gt 0} \) has a unique expansion \( n = \sum_{i=0}^{k} d_i \beta^i \) with \( 0 \le d_i \lt \beta \) and \( d_k \neq 0 \): apply the theorem to \( n \) and \( \beta \) to obtain \( d_0 \), then recurse on the quotient. Termination is the strict decrease of the quotients; uniqueness of the digits is uniqueness of the remainders.
- \( \mathbb{Z} \) is a principal ideal domain. If \( I \subseteq \mathbb{Z} \) is a non-zero ideal, let \( b \) be its least positive element; for any \( a \in I \), writing \( a = bq + r \) puts \( r = a - bq \in I \) with \( 0 \le r \lt b \), so \( r = 0 \) by minimality and \( I = b\mathbb{Z} \). Every use of the division algorithm in this argument is the descent of Step 4.
- Converse (characterisation of the quotient). If \( a = bq + r \) with \( 0 \le r \lt |b| \), then necessarily \( q = \lfloor a/b \rfloor \) for \( b \gt 0 \). So the converse direction holds and is useful: verifying the two conditions certifies that a guessed pair is the quotient and remainder, with no further computation.
- Converse that fails. “Any \( (q,r) \) with \( a = bq+r \) and \( |r| \lt |b| \) is the pair delivered by the theorem” is false: \( -17 = 5(-3) + (-2) \) satisfies \( |-2| \lt 5 \) but the theorem's pair is \( (-4, 3) \). The one-sided bound \( r \ge 0 \) is indispensable.
Fails without
- Divisor zero (\( b = 0 \)): the admissible range \( 0 \le r \lt 0 \) is empty, so no pair exists for any \( a \) whatsoever — not even for \( a = 0 \). This is not a boundary case that can be patched by a convention; the statement has no content when \( b = 0 \), which is why every implementation must trap it separately. Machine arithmetic reflects this: integer division by zero is undefined behaviour in C and raises ZeroDivisionError in Python, rather than returning a value.
- Remainder range dropped: with only \( a = bq + r \) required, the solution set is infinite — \( 100 = 7\cdot 14 + 2 = 7\cdot 13 + 9 = 7\cdot 0 + 100 \) — and “the remainder of \( 100 \) on division by \( 7 \)” is not a well-defined number. Uniqueness is a property of the constraint, not of the equation.
- Ambient ring is a field (\( \mathbb{Q}, \mathbb{R}, \mathbb{C} \)): take \( a = 1 \), \( b = 2 \) in \( \mathbb{Q} \). Both \( (q,r) = (0,1) \) and \( (q,r) = (\tfrac12, 0) \) satisfy \( a = bq+r \) with \( 0 \le r \lt 2 \), so uniqueness fails. Step 7 is the point of failure: \( |q - q'| = \tfrac12 \lt 1 \) no longer forces \( q = q' \) once \( q \) may be non-integral. Equivalently, in a field every non-zero element divides every element, so “remainder” carries no information.
- Leading coefficient not invertible (\( \mathbb{Z}[t] \)): the polynomial analogue with \( \deg \) in place of \( |\cdot| \) needs the divisor's leading coefficient to be a unit. In \( \mathbb{Z}[t] \) take \( a = t \) and \( b = 2 \), with \( \deg 2 = 0 \): we would need \( t = 2q(t) + r \) with \( r = 0 \) or \( \deg r \lt 0 \), i.e. \( t = 2q(t) \), which is impossible since \( 2 \nmid 1 \) in \( \mathbb{Z} \). Over \( \mathbb{Q}[t] \) the same division succeeds with \( q = \tfrac12 t \). The theorem is a statement about \( F[t] \) for \( F \) a field, not about polynomials in general.
- Well-ordering dropped: Step 3 needs the candidate set to attain its lower bound, which is well-ordering and not merely boundedness. Run the same argument in an ordered ring whose non-negative part is not well-ordered — \( \mathbb{Z}[x] \), ordered by leading coefficient, so that \( x \) exceeds every integer — with \( a = x \) and \( b = 2 \). Every candidate \( x - 2k \), \( k \in \mathbb{Z} \), is positive, and \( x - 2(k+1) \lt x - 2k \), so the candidate set is non-empty and bounded below by \( 0 \) yet has no least element: Step 3 has nothing to select. And no remainder exists, as it must not — \( x = 2q + r \) with \( r \in \{0,1\} \) would force \( 2 \mid 1 \) among the coefficients. Existence in this proof is entirely an application of well-ordering; no other principle is used.
Common errors
- “\( -17 \div 5 \) gives \( q = -3 \), \( r = -2 \).” That is truncation toward zero, which is what C, C++, Java, Go and Rust compute for -17 / 5 and -17 % 5. It is not the theorem's pair, because \( -2 \lt 0 \) violates \( r \ge 0 \). The theorem gives \( q = \lfloor -3.4 \rfloor = -4 \) and \( r = -17 - 5(-4) = 3 \). Python's // and % implement the floored convention and return \( -4 \) and \( 3 \).
- “\( 0 \le r \le |b| \).” The value \( r = |b| \) is never attained and admitting it destroys uniqueness at once: \( 10 = 5\cdot 1 + 5 = 5\cdot 2 + 0 \) would both be legal. The half-open interval is exactly \( |b| \) integers wide, which is the whole point.
- “For \( b \lt 0 \) the remainder is negative.” It is not: the bound is \( 0 \le r \lt |b| \) for every non-zero \( b \). Dividing \( -1387 \) by \( -360 \) gives \( q = 4 \) and \( r = 53 \), not \( r = -53 \). The remainder is a function of \( a \) and \( |b| \) alone.
- “\( q \) is \( a/b \) rounded to the nearest integer.” Rounding to nearest gives the balanced remainder of Problem 5, in the range \( (-|b|/2, |b|/2] \), not the standard one — and then only if ties are broken downward, since rounding \( a/b = 1/2 \) up would return \( r^{*} = -|b|/2 \), the excluded endpoint. For \( a = 9, b = 5 \) rounding gives \( q = 2, r = -1 \); the theorem gives \( q = 1, r = 4 \).
- Invoking well-ordering without checking \( S \neq \emptyset \). Step 2 is not a formality. A proof that writes “let \( r \) be the least non-negative value of \( a - bk \)” and moves on has assumed the conclusion for negative \( a \), where the witness genuinely has to be constructed.
- Calling it an algorithm. The proof given here is non-constructive in flavour — it takes a minimum of an infinite set — and produces no procedure. The Euclidean algorithm is an algorithm; this is the theorem that makes it terminate.
- Proving existence and forgetting uniqueness. The applications that matter most (residue systems, base-\( \beta \) digits, well-definedness of \( a \bmod n \)) turn on the uniqueness half: existence alone gives “some remainder”, never “the remainder”. A proof that stops at Step 4 has proved the less useful half.
Discussion
The result is old enough that its statement predates the vocabulary used to state it. Euclid's Elements, Book VII, Propositions 1 and 2, describes repeated subtraction of the lesser magnitude from the greater — anthyphairesis — and treats the process as evidently terminating for numbers; the modern formulation with a quantified pair \( (q,r) \) and an explicit remainder range is a nineteenth-century tidying, contemporaneous with the axiomatisation of \( \mathbb{N} \) by Dedekind and Peano that supplies the well-ordering the proof needs. What the ancients treated as a self-evident feature of magnitudes is now isolated as the one non-trivial input.
Two features of the proof are worth separating. Existence is a descent argument and is genuinely about order: the set of candidate remainders is non-empty and bounded below, and its minimum cannot be as large as \( |b| \) because one more subtraction would beat it. Uniqueness is arithmetic and is about discreteness: two admissible remainders differ by less than \( |b| \) and by a multiple of \( b \), so they do not differ at all. The two halves fail in different places, which is why the counterexamples above split into “no remainder exists” (\( b = 0 \), or \( \mathbb{Q}_{\ge 0} \) with no minimum) and “too many remainders exist” (a field, or a dropped range constraint).
The generalisation is the concept of a Euclidean domain: an integral domain \( R \) with a function \( \nu: R \setminus\{0\} \to \mathbb{Z}_{\ge 0} \) such that for all \( a \in R \) and \( b \neq 0 \) there are \( q, r \) with \( a = bq+r \) and either \( r = 0 \) or \( \nu(r) \lt \nu(b) \). Taking \( \nu = |\cdot| \) recovers this theorem; \( \nu = \deg \) gives polynomial long division over a field; \( \nu(x+iy) = x^2+y^2 \) gives division in the Gaussian integers \( \mathbb{Z}[i] \). Note what the general definition drops: uniqueness. In \( \mathbb{Z}[i] \), dividing \( 3+4i \) by \( 2 \) admits several remainders of norm less than \( N(2) = 4 \), and no canonical choice is singled out. Uniqueness is a luxury of the totally ordered case, and the Euclidean algorithm does not need it — only the strict decrease of \( \nu \) matters for termination. Every Euclidean domain is a principal ideal domain by the argument in the Corollaries, and hence a unique factorisation domain; the converses both fail, \( \mathbb{Z}\big[\tfrac{1+\sqrt{-19}}{2}\big] \) being the standard example of a principal ideal domain that is not Euclidean.
The choice of remainder range is a genuine design decision with consequences, and computing has had to make it explicitly. Three conventions are in use for \( a = bq + r \): floored division, \( q = \lfloor a/b \rfloor \), giving \( r \) the sign of \( b \) — the convention of this theorem when \( b \gt 0 \), and the one implemented by Python's // and %; truncated division, \( q \) rounded toward zero, giving \( r \) the sign of \( a \) — the convention mandated by C99 and inherited by most of its descendants; and Euclidean division, \( 0 \le r \lt |b| \) for both signs of \( b \), which is the theorem exactly as stated here. Knuth argues in The Art of Computer Programming for the floored convention on the grounds that it makes \( r \) a periodic function of \( a \) with period \( b \), so that \( a \bmod b \) is compatible with congruence; the truncated convention breaks that periodicity at \( a = 0 \). Ada exposes both, as mod (floored) and rem (truncated). For \( b \gt 0 \) — the case that arises in number theory — floored and Euclidean coincide, which is why the distinction rarely surfaces in mathematics and constantly surfaces in code.
Common misconceptions. (i) That the theorem asserts something about the decimal expansion of \( a/b \) — it does not; it is a statement in \( \mathbb{Z} \) that mentions no rational number, and the closed form \( q = \lfloor a/b\rfloor \) is a corollary derived after the fact in Step 9. (ii) That the divisor must be smaller than the dividend — nothing of the kind is assumed; \( 3 = 100\cdot 0 + 3 \) is the correct division of \( 3 \) by \( 100 \), with \( q = 0 \). (iii) That “the remainder” is a property of \( a \) and \( b \) independent of the convention — it is a property of \( a \), \( b \), and the chosen interval of length \( |b| \); the theorem fixes that interval to be \( [0,|b|) \), and the balanced convention of Problem 5 is an equally valid theorem with a different answer.
Worked examples
Example 1. A spacecraft mission clock reports an elapsed time of \( 100\,000 \) seconds since launch. Express this as days, hours, minutes and seconds, and justify each step by the division algorithm.
Reading. Mixed-radix time notation is the division algorithm applied three times, each application supplying one field. Every digit lies in its stated range because the theorem forces \( 0 \le r \lt |b| \), and the representation is unique for the same reason.
Scope. Exact for any non-negative integer number of seconds. For a negative elapsed time (a count-down clock reading \( T \lt 0 \)) the same three divisions apply unchanged and still return non-negative fields, but the day count becomes negative — see Example 2.
Example 2. An inertial platform integrates its yaw rate and reports a cumulative heading of \( -1387^{\circ} \). Reduce this to a compass heading in \( [0^{\circ}, 360^{\circ}) \). Then repeat the reduction with the divisor \( b = -360 \), as would arise from a sensor that counts clockwise turns as negative, and confirm the closed forms of Step 9.
Reading. The remainder \( 53^{\circ} \) is the same for \( b = +360^{\circ} \) and \( b = -360^{\circ} \); only the sign of the revolution count changes. A negative dividend does not produce a negative remainder.
Scope. Exact for integer degrees. For a real-valued heading the same reduction is \( \theta - 360^{\circ}\lfloor \theta/360^{\circ}\rfloor \), which is the floored convention extended to \( \mathbb{R} \); it is a definition there, not a theorem, since uniqueness of \( (q,r) \) over \( \mathbb{R} \) requires \( q \) to be constrained to \( \mathbb{Z} \) by fiat.
Problems
- Find the quotient and remainder of \( a = 2026 \) on division by \( b = 45 \), and verify both conditions of the theorem.
Solution
Estimate \( q \) by Step 9: \( 2026/45 = 45.0\overline{2} \), so \( q = \lfloor 45.02\ldots \rfloor = 45 \). Then \( r = a - bq = 2026 - 45\cdot 45 = 2026 - 2025 = 1 \). Both conditions hold: \( 45\cdot 45 + 1 = 2026 \), and \( 0 \le 1 \lt 45 \). By uniqueness, \( (q,r) = (45,1) \) and no other pair works. Incidentally \( r \neq 0 \), so \( 45 \nmid 2026 \).
- Repeat the previous problem for \( a = -2026 \) with \( b = 45 \), and then with \( b = -45 \). State the remainder in each case.
Solution
For \( b = 45 \): \( -2026/45 = -45.02\overline{2} \), so \( q = \lfloor -45.02\ldots\rfloor = -46 \) (not \( -45 \): the floor of a negative non-integer goes down). Then \( r = -2026 - 45(-46) = -2026 + 2070 = 44 \), and \( 0 \le 44 \lt 45 \). So \( -2026 = 45\cdot(-46) + 44 \).
For \( b = -45 \): by Step 8 the remainder is unchanged and the quotient flips, giving \( q = 46 \), \( r = 44 \). Check: \( (-45)(46) + 44 = -2070 + 44 = -2026 \), and \( 0 \le 44 \lt |-45| = 45 \). Equivalently \( q = \lceil -2026/-45 \rceil = \lceil 45.02\ldots\rceil = 46 \).
The remainder is \( 44 \) in both cases. Note the contrast with Problem 1: negating the dividend does not negate the remainder; \( 1 \) becomes \( 44 = 45 - 1 \).
- Show that \( n^2 \equiv 0 \) or \( 1 \pmod 4 \) for every \( n \in \mathbb{Z} \), and deduce that no integer of the form \( 4k+3 \) is a sum of two squares. Illustrate with \( 2027 \).
Solution
By the division algorithm with \( b = 2 \), every \( n \) is uniquely \( n = 2q + r \) with \( r \in \{0,1\} \) — existence gives the two cases, uniqueness guarantees they are exhaustive and disjoint. If \( r = 0 \) then \( n^2 = 4q^2 \equiv 0 \pmod 4 \). If \( r = 1 \) then \( n^2 = 4q^2 + 4q + 1 = 4(q^2+q) + 1 \equiv 1 \pmod 4 \). So \( n^2 \bmod 4 \in \{0,1\} \).
Hence for any \( m, n \), the sum \( m^2 + n^2 \) is congruent mod \( 4 \) to one of \( 0+0 = 0 \), \( 0+1 = 1 \), \( 1+1 = 2 \) — never \( 3 \). Any integer of the form \( 4k+3 \) is therefore not a sum of two integer squares.
Illustration: \( 2027 = 4\cdot 506 + 3 \) (since \( 4\cdot 506 = 2024 \) and \( 0 \le 3 \lt 4 \)), so \( 2027 \) is not a sum of two squares. Checking by hand would require testing \( m \le \lfloor\sqrt{2027}\rfloor = 45 \); the congruence settles it in one division.
- Prove the closed forms of Step 9 directly from the theorem: if \( a = bq+r \) with \( 0 \le r \lt |b| \), then \( q = \lfloor a/b\rfloor \) when \( b \gt 0 \) and \( q = \lceil a/b\rceil \) when \( b \lt 0 \). Verify both with \( a = -1387 \), \( |b| = 360 \).
Solution
Let \( b \gt 0 \). From \( a = bq + r \) we get \( a - bq = r \), and the hypothesis \( 0 \le r \lt b \) becomes \( 0 \le a - bq \lt b \). Dividing by \( b \gt 0 \) preserves both inequalities: \( 0 \le a/b - q \lt 1 \), i.e. \( q \le a/b \lt q+1 \). Since \( q \in \mathbb{Z} \), this is exactly the defining property of the floor, so \( q = \lfloor a/b\rfloor \).
Let \( b \lt 0 \), so \( |b| = -b \). The hypothesis reads \( 0 \le a - bq \lt -b \). Dividing by \( b \lt 0 \) reverses both inequalities: \( 0 \ge a/b - q \gt -1 \), i.e. \( q - 1 \lt a/b \le q \). Since \( q \in \mathbb{Z} \), that is the defining property of the ceiling, so \( q = \lceil a/b\rceil \).
Check with \( a = -1387 \). For \( b = 360 \): \( a/b = -3.8527\ldots \), \( \lfloor a/b\rfloor = -4 \), and indeed \( -1387 = 360(-4) + 53 \) with \( 0 \le 53 \lt 360 \). For \( b = -360 \): \( a/b = 3.8527\ldots \), \( \lceil a/b \rceil = 4 \), and \( -1387 = (-360)(4) + 53 \) with \( 0 \le 53 \lt 360 \). Both closed forms return the quotients found in Example 2.
- (Balanced, or least-absolute, remainders.) Let \( b \gt 0 \). Prove that for every \( a \in \mathbb{Z} \) there are unique \( q^{*}, r^{*} \in \mathbb{Z} \) with \( a = bq^{*} + r^{*} \) and \( -b/2 \lt r^{*} \le b/2 \). Compute \( (q^{*}, r^{*}) \) for \( a = 95, b = 8 \) and for \( a = 100, b = 8 \), and compare with the standard pair.
Solution
Existence. Apply the theorem to get \( a = bq + r \) with \( 0 \le r \lt b \). If \( r \le b/2 \), set \( (q^{*}, r^{*}) = (q, r) \); then \( r^{*} \ge 0 \gt -b/2 \), so the range holds. If \( r \gt b/2 \), set \( (q^{*}, r^{*}) = (q+1,\ r-b) \); then \( a = b(q+1) + (r-b) \) still holds identically, and \( r \gt b/2 \) gives \( r^{*} = r-b \gt -b/2 \) while \( r \lt b \) gives \( r^{*} \lt 0 \le b/2 \). Either way the range \( -b/2 \lt r^{*} \le b/2 \) is met.
Uniqueness. The half-open interval \( (-b/2,\ b/2] \) has length exactly \( b \), so it contains exactly \( b \) integers (for \( b \) even, \( -b/2+1, \ldots, b/2 \); for \( b \) odd, \( -(b-1)/2, \ldots, (b-1)/2 \) — in both cases \( b \) values). If \( bq_1 + r_1 = bq_2 + r_2 \) with both remainders in that interval, then \( b(q_1-q_2) = r_2-r_1 \) and \( |r_2 - r_1| \lt b \) because two points of an interval of length \( b \) differ by less than \( b \) — strictly, since one endpoint is excluded. Hence \( b|q_1-q_2| \lt b \), so \( |q_1-q_2| \lt 1 \), so \( q_1 = q_2 \) and then \( r_1 = r_2 \). This is Steps 5–7 verbatim with the interval shifted.
Numbers. For \( a = 95, b = 8 \): standard division gives \( 95 = 8\cdot 11 + 7 \), and \( 7 \gt 8/2 = 4 \), so shift: \( q^{*} = 12 \), \( r^{*} = 7 - 8 = -1 \). Check \( 8\cdot 12 - 1 = 95 \) and \( -4 \lt -1 \le 4 \). For \( a = 100, b = 8 \): \( 100 = 8\cdot 12 + 4 \), and \( 4 \le 8/2 = 4 \), so no shift is needed: \( (q^{*}, r^{*}) = (12, 4) \). The boundary case \( r = b/2 \) is kept, not shifted — that is precisely the choice that makes the interval half-open on the correct side and preserves uniqueness.