Divide and conquer example: multiplication [00PR]
- October 21, 2026
The material in this section is presented via a POGIL activity.
Another classic place where divide-and-conquer comes up is in trying to multiply large numbers faster than the naïve algorithm you learned in grade school.
Motivation [00PS]
- October 21, 2026
In many situations, when we use arithmetic operations on integers such as addition or multiplication, we simply assume they take constant time. This is appropriate when the integers are bounded by some fixed size, e.g. if all the integers are 64-bit integers. However, if we want to be able to deal with integers of arbitrary size, this is no longer appropriate; we must take the size of the integers into account. For example, as of October 20, 2026, the largest known prime number is 2^{136\,279\,841} - 1, a number with 41\,024\,320 decimal digits. The arithmetic operations needed to test this number for primality take a nontrivial amount of time!
n-bit addition [00PT]
- October 22, 2026
Adding two n-bit numbers is easily analyzed: we iterate through, starting with the ones place, and add the bits in each place (possibly doing another addition for a carry bit), for a total of about 2n bit operations in the worst case. Hence we know it is possible to add two n-bit numbers in linear time, so the worst-case time to add is O(n). However, it is also clearly \Omega (n), since we have to at least look at every bit in the input. Hence, adding n-bit numbers takes \Theta (n) time.
Naïve n-bit multiplication [00PU]
- October 22, 2026
What about multiplication? Using the standard "grade school" algorithm to compute p \times q, we end up with a shifted copy of p for every 1 bit in q, and then we must add then all. So there are up to n shifted copies of p to add, with each being at most 2n bits, so this particular algorithm will take \Theta (n^2) in the worst case.
However, unlike addition which obviously takes \Omega (n) time, it is not so obvious that multiplication must take \Omega (n^2) time. In fact, if anything, it is the opposite: it seems like adding up a bunch of shifted copies of the same bitstring might involve some redundant work. Perhaps there is a way we can multiply two n-bit numbers in faster than quadratic time.
Divide and conquer multiplication [00PV]
- October 22, 2026
Given an n-bit number X (and assuming n is even), we can split X into two halves X_1 and X_2, each with n/2 bits. We can express this as X = 2^{n/2} X_1 + X_2 since multiplying by a power of two performs a bit shift, i.e. adding zeros to the end of a number in binary (just like multiplying by a power of ten adds zeros to the end of a number in decimal—and for the same reason).
Remark What if n is odd?
- October 22, 2026
Note that we can still do something similar if n is odd—for example we can write X = 2^{\lceil n/2 \rceil } X_1 + X_2 where X_2 is one digit longer than X_1 (or we can imagine "padding" X_1 to length \lceil n/2 \rceil by adding a zero digit on the left).
If we have two n-bit numbers expressed in this form and multiply them, we get AB = (2^{n/2} A_1 + A_2)(2^{n/2} B_1 + B_2) = 2^n A_1 B_1 + 2^{n/2}(A_1 B_2 + A_2 B_1) + A_2 B_2. On the left-hand side we have a single n-bit multiplication; on the right-hand side we have four n/2-bit multiplications, two shifts, and three n-bit additions. We can turn this into a recursive algorithm for computing the product AB, something like the following:
(This works when n is a power of two; it is not hard (though a bit fiddly) to generalize to any value of n.)
Analysis of naïve divide-and-conquer multiplication [00PX]
- October 23, 2026
This divide-and-conquer multiplication algorithm involves four recursive calls to \mathsf {mul} on inputs of size n/2, two shifts (which take constant time), and three n-bit additions (which take \Theta (n)). Hence we can write a recurrence for the time M(n) taken by \mathsf {mul} on n-bit inputs: \begin {align*} M(1) &= \Theta (1) \\ M(n) &= 4M(n/2) + \Theta (n) \end {align*} But what is a closed form for M(n)? Is it better than n^2?
We can unroll this recurrence into a recursion tree to figure out how much total work happens.
- At the root, an amount of work happens proportional to the input size, n.
- At the first level of recursion, there are four recursive calls, each on n/2-bit numbers, giving a total something like 4 \cdot (n/2) = 2n.
- At the second level, there are sixteen recursive calls on n/4-bit inputs, giving a total of 4n.
- In general, at depth k, there are 4^k recursive calls, each on an input of size n/2^k, for a total of 4^k \cdot n/2^k = 2^k n.