The Karatsuba algorithm is a fast multiplication algorithm. It was discovered by Anatoly Karatsuba in 1960 and published in 1962. It reduces the multiplication of two n-digit numbers to at most
Contents
- History
- Basic step
- Example
- Recursive application
- Asymmetric Karatsuba like formulae
- Efficiency analysis
- Pseudocode
- References
The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the quadratic "grade school" algorithm. The Toom–Cook algorithm is a faster generalization of Karatsuba's method, and the Schönhage–Strassen algorithm is even faster, for sufficiently large n.
History
The standard procedure for multiplication of two n-digit numbers requires a number of elementary operations proportional to
In 1960, Kolmogorov organized a seminar on mathematical problems in cybernetics at the Moscow State University, where he stated the
Basic step
The basic step of Karatsuba's algorithm is a formula that allows one to compute the product of two large numbers
Let
where
where
These formulae require four multiplications, and were known to Charles Babbage. Karatsuba observed that
which holds since
A more efficient implementation of Karatsuba multiplication can be set as
Example
To compute the product of 12345 and 6789, choose B = 10 and m = 3. Then we decompose the input operands using the resulting base (Bm = 1000), as:
12345 = 12 · 1000 + 3456789 = 6 · 1000 + 789Only three multiplications, which operate on smaller integers, are used to compute three partial results:
z2 = 12 × 6 = 72z0 = 345 × 789 = 272205z1 = (12 + 345) × (6 + 789) − z2 − z0 = 357 × 795 − 72 − 272205 = 283815 − 72 − 272205 = 11538We 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 · B2m + z1 · Bm + z0, 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; but note also that this partial result z1 cannot be negative: to compute these subtractions, equivalent additions using complements to 10002 can also be used, keeping only the two least significant base-1000 digits for each number:
z1 = 283815 − 72 − 272205 = (283815 + 999928 + 727795) mod 10002 = 2011538 mod 10002 = 11538.Recursive application
If n is four or more, the three multiplications in Karatsuba's basic step involve operands with fewer than n digits. Therefore, those products can be computed by recursive calls of the Karatsuba algorithm. The recursion can be applied until the numbers are so small that they can (or must) be computed directly.
In a computer with a full 32-bit by 32-bit multiplier, for example, one could choose B = 231 = 2,147,483,648, and store each digit as a separate 32-bit binary word. Then the sums x1 + x0 and y1 + y0 will not need an extra binary word for storing the carry-over digit (as in carry-save adder), and the Karatsuba recursion can be applied until the numbers to multiply are only one digit long.
Asymmetric Karatsuba-like formulae
Karatsuba's original formula and other generalizations are themselves symmetric. For example, the following formula computes
with 6 multiplications in
where
This formula is symmetrical, namely, it does not change if we exchange
Based on the second Generalized division algorithms , Fan et al. found the following asymmetric formula:
where
It is asymmetric because we can obtain the following new formula by exchanging
where
Efficiency analysis
Karatsuba's basic step works for any base B and any m, but the recursive algorithm is most efficient when m is equal to n/2, rounded up. In particular, if n is 2k, for some integer k, and the recursion stops only when n is 1, then the number of single-digit multiplications is 3k, which is nc where c = log23.
Since one can extend any inputs with zero digits until their length is a power of two, it follows that the number of elementary multiplications, for any n, is at most
Since the additions, subtractions, and digit shifts (multiplications by powers of B) in Karatsuba's basic step take time proportional to n, their cost becomes negligible as n increases. More precisely, if t(n) denotes the total number of elementary operations that the algorithm performs when multiplying two n-digit numbers, then
for some constants c and d. For this recurrence relation, the master theorem gives the asymptotic bound
It follows that, for sufficiently large n, Karatsuba's algorithm will perform fewer shifts and single-digit additions than longhand multiplication, even though its basic step uses more additions and shifts than the straightforward formula. For small values of n, however, the extra shift and add operations may make it run slower than the longhand method. The point of positive return depends on the computer platform and context. As a rule of thumb, Karatsuba is usually faster when the multiplicands are longer than 320–640 bits.