Fast multiplication

To compute the product of 12345 and 6789, where B = 10, choose m = 3. Then we decompose the input operands using the resulting base (Bm = 1000), as:
 * 12345 = 12 · 1000 + 345
 * 6789 = 6 · 1000 + 789

Only three multiplications, which operate on smaller integers, are used to compute three partial results:
 * z2 = 12 × 6 = 72
 * z0 = 345 × 789 = 272205
 * z1 = (12 + 345) × (6 + 789) − z2 − z0 = 357 × 795 − 72 − 272205 = 283815 − 72 − 272205 = 11538

We get the result by just adding these three partial results, shifted accordingly (and then taking carries into account by decomposing these three inputs in base 1000 like for the input operands):
 * result = z2 · (Bm)2 + z1 · (Bm)1 + z0 · (Bm)0, i.e.
 * result = 72 · 10002 + 11538 · 1000 + 272205 = 83810205.

Note that the intermediate third multiplication operates on an input domain which is less than two times larger than for the two first multiplications, its output domain is less than four times larger, and base-1000 carries computed from the first two multiplications must be taken into account when computing these two subtractions.

Explanation
The basic step of Karatsuba's algorithm is a formula that allows one to compute the product of two large numbers $$x$$ and $$y$$ using three multiplications of smaller numbers, each with about half as many digits as $$x$$ or $$y$$, plus some additions and digit shifts. This basic step is, in fact, a generalization of a similar complex multiplication algorithm, where the imaginary unit $i$ is replaced by a power of the base.

Let $$x$$ and $$y$$ be represented as $$n$$-digit strings in some base $$B$$. For any positive integer $$m$$ less than $$n$$, one can write the two given numbers as


 * $$x = x_1 B^m + x_0,$$
 * $$y = y_1 B^m + y_0,$$

where $$x_0$$ and $$y_0$$ are less than $$B^m$$. The product is then


 * $$xy = (x_1 B^m + x_0)(y_1 B^m + y_0),$$
 * $$xy = z_2 B^{2m} + z_1 B^m + z_0,$$

where


 * $$z_2 = x_1 y_1,$$
 * $$z_1 = x_1 y_0 + x_0 y_1,$$
 * $$z_0 = x_0 y_0.$$

These formulae require four multiplications and were known to Charles Babbage. Karatsuba observed that $$xy$$ can be computed in only three multiplications, at the cost of a few extra additions. With $$z_0$$ and $$z_2$$ as before one can observe that


 * $$z_1 = (x_1 + x_0)(y_1 + y_0) - z_2 - z_0.$$

An issue that occurs, however, when computing $$z_1$$ is that the above computation of $$(x_1 + x_0)$$ and $$(y_1 + y_0)$$ may result in overflow (will produce a result in the range $$0 \leq \text{result} < 2 B^m$$), which require a multiplier having one extra bit. This can be avoided by noting that


 * $$z_1 = (x_0 - x_1)(y_1 - y_0) + z_2 + z_0.$$

This computation of $$(x_0 - x_1)$$ and $$(y_1 - y_0)$$ will produce a result in the range of $$-B^m < \text{result} < B^m$$. This method may produce negative numbers, which require one extra bit to encode signedness, and would still require one extra bit for the multiplier. However, one way to avoid this is to record the sign and then use the absolute value of $$(x_0 - x_1)$$ and $$(y_1 - y_0)$$ to perform an unsigned multiplication, after which the result may be negated when both signs originally differed. Another advantage is that even though $$(x_0 - x_1)(y_1 - y_0)$$ may be negative, the final computation of $$z_1$$ only involves additions.