CSE 101 Notes
Large Integer Multiplication Using Divide-and-Conquer

The ``grade school'' algorithm for multiplying n-digit integers x and y multiplies every digit in x by every digit in y, yielding a algorithm. We want to do better using a divide-and-conquer method. We assume the integers are represented in binary.

For the ``divide'' step, we split each binary integer into two roughly equal-sized parts, each containing about half of the bits of the original: one part contains the high order bits, and the other part contains the low order bits. Specifically, if x and y have n bits, we let and divide x into its low-order m bits (), and its high-order n-m bits (); and are defined similarly. These are given by , , and similarly for y. These operations can be implemented by zeroing x's low-order bits and shifting the remaining bits down to get , and by simply zeroing x's high-order bits to get . This shifting and zeroing of bits can be done in linear time in the number of bits, so this ``divide'' step takes time linear in n.

Note that this decomposition is such that we have:

and

So that:

This specifies the product xy that we want, in terms of products of smaller integers - these smaller products can be computed with recursive calls. The above formula for xy makes it look as though we need to do four recursive calls (one each for , , , and ) - but if we write down that straightforward divide-and-conquer algorithm we get a method that is no better than the ``grade school'' algorithm. But, we can do better than this.

First, do two recursive calls to compute and . Now, note that

so that we can compute this product with one additional multiply, and then get the remaining term we need:

without doing any additional multiplies. That is, we have done three multiplies (plus some additions, which only take linear time in the number of bits), and we have obtained all the information we need to compute xy according to the formula above. This is given in detail in the algorithm below.

The complete running time analysis is a bit tricky, so let's do a rough analysis first to get some idea of the running time. Roughly, for integers of size n, we decompose them into integers of size and do 3 recursive calls plus a linear amount of extra work (using linear-time shift, bit-zeroing, and addition operations as discussed above). Calling our approximately analyzed time , this would give

Using the master theorem for recurrences (page 62 of the text): we have b=2, a=3, and is . So, . Since is about , we have that is for (picking a somewhat arbitrary value) , and therefore we are in case 1 of the master theorem, and

Unfortunately, when computing a sum of two bit integers, such as , carry may result in a sum that has bits rather than just . Thus, some subproblem sizes are a little larger than the above analysis suggests. Simplify the analysis by assuming that ALL subproblems are of size (which doesn't change the asymptotic running time, and which can be easily achieved by padding subproblems with an extra leading zero bit, if necessary). Then our actual running time is

We solve this using an intermediate function , which we define to be equal to . Now,

Taking the first and last lines of this, we have

so by the analysis in the above paragraph. by the definition of , so