Definition:Classical Algorithm/Subtraction

Definition
Let $u = \sqbrk {u_{n - 1} u_{n - 2} \dotsm u_1 u_0}_b$ and $v = \sqbrk {v_{n - 1} v_{n - 2} \dotsm v_1 v_0}_b$ be $n$-digit integers.

The classical subtraction algorithm forms their $n$-digit difference $u - v$:
 * $w = \sqbrk {w_n w_{n - 1} \dotsm w_1 w_0}_b$

where $w_n$ is either $0$ or $1$.

The steps are:
 * $(\text S 1): \quad$ Set $j = 0$, $k = 0$.
 * $j$ is used to run through all the digit positions
 * $k$ keeps track of the carry digit between each step.


 * $(\text S 2): \quad$ Calculate digit $j$:
 * Calculate $\begin {cases} d = \paren {u_j + v_j - k} \pmod b \\ c = \floor {\dfrac {u_j - v_j + k} b} \end {cases}$ using the primitive subtraction.
 * Set $w_j$ to $d$.
 * Set $k$ to $c$.


 * $(\text S 3): \quad$ Add $1$ to $j$, using conventional integer addition.
 * If $j < n$, return to $(\text S 2)$.
 * Otherwise exit.

Finiteness
For each iteration through the algorithm, step $(\text S 3)$ is executed, which increases $j$ by $1$.

Hence also by step $(\text S 3)$, the algorithm will terminate after $n$ iterations.

Definiteness

 * Step $(\text S 1)$: Trivially definite.


 * Step $(\text S 2)$: The primitive subtraction operation is precisely defined.


 * Step $(\text S 3)$: Trivially definite.

Inputs
The inputs to this algorithm are the $n$-digit integers $u = \sqbrk {u_{n - 1} u_{n - 2} \dotsm u_1 u_0}_b$ and $v = \sqbrk {v_{n - 1} v_{n - 2} \dotsm v_1 v_0}_b$.

Outputs
The output from this algorithm is the $n + 1$-digit integer $w = \sqbrk {w_{n - 1} \dotsm w_1 w_0}_b$.

Effective
Each step of the algorithm is basic enough to be done exactly and in a finite length of time.

Hence by definition of algorithm, the classical subtraction algorithm meets the criteria for it to be an algorithm.

Also see

 * Classical Subtraction Algorithm is Valid, which demonstrates that $u - v = w$.