Division Algorithm

Given two integers a and b, with b0, there exist unique integers q and r such that $$a=bq+r$$ and 0r<|b|, where |b| is the absolute value of b.

a is called the dividend,
b is called the divisor,
q is called the quotient,
r is called the remainder

function divide_unsigned(A,B)
    if B = 0 then error(DivisionByZero) end
    R := A
    Q := 0
    while R ≥ B do
        R := R − B
        Q := Q + 1
    end
    return (Q, R)
end

Proof

Existence

Suppose (without loss of generality) that b>0

There exist integers q1,r10 such that a=bq1+r1 :

  1. a0 :
    q1=0 and r1=a
  2. a<0 :
    q1=a and r1=aab

Let q and r be such integers.
If rb :
a=b(q+1)+(rb), with 0rb<r,
so r is not minimal

Then the algorithm for finding a minimal r is:

  1. Start with q=0
  2. Increment by 1 until abq<b
Uniqueness

Let q,r be a quotient and remainder pair.

By definition,

0r<|b|0r<|b|

Thus, |rr|<|b|.

By definition,

a=bq+ra=bq+r

Setting these equal to each other,

bq+r=bq+rb(qq)=rr

so b is a divisor of (rr).

However, as established, |rr|<|b| ;
then, necessarily, rr=0 and qq=0,
so r=r and q=q.

QED.


Powered by Forestry.md