Today we will work with a divide-and-conquer algorithm for integer multiplication from section 5.5 of KT. Of course in most cases in the real world, multiplication takes O(1) time, because our machines are built to multiply two 32-bit or 64-bit integers by a primitive operation that can be executed in O(1) clock cycles. But it's worthwhile to examine the way that the time to multiply two numbers depends on their length. An integer multiplication must eventually be implemented by bit operations, so looking at how many such operations we need to multiply n-bit integers can be important for designing the multiplication circuits that operate inside the machine. Also, we may want to multiply very long integers that are stored as sequences of several 32-bit or 64-bit components. Doing this will require implementing the multiplication in terms of additions and multiplications of single components, which is vey similar to implementing integer multiplication in terms of bits. What we'll do today is improve on the standard algorithm that takes O(n2) basic operations.
The basic trick is simple. Given two integers x1 and x2, each of at most n bits, we write x1 = a12n/2 + b1 and x2 = a22n/2 + b2. Then we have:
x1x2 = (a1a2)2n + (a1b2 + a2b1)2n/2 + b1b2.
which suggests a recursive attack -- we can multiply the n-bit numbers using four n/2-bit multiplications. This gives the recurrence T(n) = 4T(n/2) + O(n), but this solves only to T(n) = O(n2).
But we can do better. Note that a1b2 = (a1 + b1)(a2b1) - a1a2 - b1b2. Since we have to compute a1a2 and b1b2 anyway, we only need one more multiplication (and two subtractions) to get the middle term of our some. This gives us T(n) = 3T(n/2) + O(n), which solves to T(n) = O(n1.59) as discussed in section 5.2 of KT.
Ordinary method first:
Now for the fun part. Our three four-bit multiplications are:
Now the final result is 111100000000000 + (11111101 - 1111000 - 1011)*10000
+ 1011. This is 111100000000000 + 11110100000 + 1011 = 111111110101011, as
desired. (Decimal check: 120*256 + (253 - 120 - 11)*16 + 11 = 32683.)
10100001
* 11001011
-----------
10100001
10100001
00000000
10100001
00000000
00000000
10100001
10100001
------------------
111111110101011 decimal sanity check: 161 * 203 = 32683
(Note that to multiply two numbers of 2n bits each by the new method, we need two shifts, three multiplications of n bits, two additions of numbers up to 4n bits, and two subtractions of numbers of 2n bits. This gives us a recurrence of T(2n) = 3T(n/2) + 14n + 2, with a base case of T(1) = 1. An easy and only slightly conservative way to describe the old method is that we compute n2 individual products, shift each one into place, and add the results together. Actually this would be n2 - 1 additions of n-word numbers which would be O(n3), so we need to do better. If we multiply the first number by each component of the other number, taking 2n to do each one, then shift these n n-word numbers into place and add them, we take n*2n + (n-1) + 2n(n-1) = 4n2 - n - 1. Use this for your estimate.
The recurrence first:
The ordinary method:
Last modified 19 October 2006