[This page is auto-generated; please prefer to read the article’s PDF Form.]
Let us first understand the multiplication of a by any single digit s of b. It is done iteratively by picking each digit ai, starting at the LSD (position 0) and computing ais. Since,
|
so the carry produced by position 0, c0, can be maximum (s − 1). For the computation at position 1, we have:
|
So, the carry produced by position 1, c1, also can be maximum (s− 1). Thus, by repeating such argument we know that the carry ci produced by any position i can never exceed (s − 1).
Lemma 1. In the schoolbook multiplication algorithm, when multiplying operand a with digit s in any base B, the carry produced by any position cannot exceed (s − 1). And since s ≤ (B − 1), a carry can never exceed (B − 2).
This allows us to conclude that such a carry can be stored and added like any other digit of our base B numeral system, using the uint type.
Note that the calculation at any position i is of the form (defining c−1 = 0): ci−1 + ais, which can produce values of up to 2-digit numeral in base B, because:
|
[Side Note] At all positions except the MSD position of a (i.e. i = na − 1), this calculation contributes the lower digit of this 2-digit value in the result (a⋅s), sending the upper digit to carry. At position i = na − 1, it will contribute both of its digits in the result. Now we will write a lemma which will be useful later when discussing Division operation.
Lemma 2. In the schoolbook multiplication algorithm, when multiplying operand a = (ana−1ana−2…a1a0) with digit s in any base B, the result (a⋅s) has either na or na + 1 digits. In the result, the two digits at (most-significant) positions na, na − 1 are produced by the last iteration, which multiplies MSD of a with s. This 2-digit value is given by (cna−2 is the earlier produced carry):
|
[End of Side Note]
Since the calculation at any position can produce values only up to 2-digit, they should fit in the “unsigned long” type.
Copyright © 2021 Nitin Verma. All rights reserved.