[This page is auto-generated; please prefer to read the article’s PDF Form.]
Now we know how to produce each row Ri. Instead of first producing and storing all such rows, and then adding them all (after left-shifting), we will add each row to a common result r as and when it is produced. Thus we will avoid the need to store these rows for later processing.
Below method multiply_digit() implements multiplication of a bigint by a single digit, and multiply() implements multiplication of two bigints. Please refer to [1] for implementation of methods referred by these methods.
Note that each row Ri has up to na + 1 digits, and after left-shifting it by i positions, it effectively has na + i + 1 digits. When we add row Ri to the common result r, we will be performing an addition involving na + i + 1 digit-positions (the current r must not be having more digits than this); i.e. na + i + 1 iterations in the addition method. Starting with r as R0, the addition of remaining nb − 1 rows will require total such iterations as:
|
So when na < nb, we can reduce these iterations by swapping a and b. That is, for efficiency, the operand with more number of digits will always be chosen as the upper operand.
Copyright © 2021 Nitin Verma. All rights reserved.