Karatsuba algorithm


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 single-digit multiplications in general. It is therefore faster than the traditional algorithm, which requires single-digit products. For example, the Karatsuba algorithm requires 310 = 59,049 single-digit multiplications to multiply two 1024-digit numbers, whereas the traditional algorithm requires 2 = 1,048,576.
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, or in big-O notation. Andrey Kolmogorov conjectured that the traditional algorithm was asymptotically optimal, meaning that any algorithm for that task would require elementary operations.
In 1960, Kolmogorov organized a seminar on mathematical problems in cybernetics at the Moscow State University, where he stated the conjecture and other problems in the complexity of computation. Within a week, Karatsuba, then a 23-year-old student, found an algorithm that multiplies two n-digit numbers in elementary steps, thus disproving the conjecture. Kolmogorov was very excited about the discovery; he communicated it at the next meeting of the seminar, which was then terminated. Kolmogorov gave some lectures on the Karatsuba result at conferences all over the world and published the method in 1962, in the Proceedings of the USSR Academy of Sciences. The article had been written by Kolmogorov and contained two results on multiplication, Karatsuba's algorithm and a separate result by Yuri Ofman; it listed "A. Karatsuba and Yu. Ofman" as the authors. Karatsuba only became aware of the paper when he received the reprints from the publisher.

Algorithm

Basic step

The basic step of Karatsuba's algorithm is a formula that allows one to compute the product of two large numbers and using three multiplications of smaller numbers, each with about half as many digits as or, plus some additions and digit shifts. This basic step is, in fact, a generalization of a similar complex multiplication algorithm, where the imaginary unit is replaced by a power of the base.
Let and be represented as -digit strings in some base. For any positive integer less than, one can write the two given numbers as
where and are less than. The product is then
where
These formulae require four multiplications and were known to Charles Babbage. Karatsuba observed that can be computed in only three multiplications, at the cost of a few extra additions. With and as before one can observe that
An issue that occurs, however, when computing is that the above computation of and may result in overflow, which require a multiplier having one extra bit. This can be avoided by noting that
This computation of and will produce a result in the range of. 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 and to perform an unsigned multiplication, after which the result may be negated when both signs originally differed. Another advantage is that even though may be negative, the final computation of only involves additions.

Example

To compute the product of 12345 and 6789, where B = 10, choose m = 3. We use m right shifts for decomposing the input operands using the resulting base, as:
Only three multiplications, which operate on smaller integers, are used to compute three partial results:
We get the result by just adding these three partial results, shifted accordingly :
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.

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 be computed directly.
In a computer with a full 32-bit by 32-bit multiplier, for example, one could choose B = 231 =, 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, 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 is the Galois field with two elements 0 and 1.
where and.
We note that addition and subtraction are the same in fields of characteristic 2.
This formula is symmetrical, namely, it does not change if we exchange and in and.
Based on the second Generalized division algorithms
, Fan et al. found the following asymmetric formula:
where
and
It is asymmetric because we can obtain the following new formula by exchanging and in
and.
where
and .

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 in Karatsuba's basic step take time proportional to n, their cost becomes negligible as n increases. More precisely, if t 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 for divide-and-conquer recurrences 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's method is usually faster when the multiplicands are longer than 320–640 bits.

Pseudocode

Here is the pseudocode for this algorithm, using numbers represented in base ten. For the binary representation of integers, it suffices to replace everywhere 10 by 2.
It's important to note that the "split_at" function works as follows: split_at returns: high="12", low="345".

procedure karatsuba
if or
return num1 × num2

/* Calculates the size of the numbers. */
m = min, size_base10)
m2 = floor
/*m2 = ceil will also work */

/* Split the digit sequences in the middle. */
high1, low1 = split_at
high2, low2 = split_at

/* 3 calls made to numbers approximately half the size. */
z0 = karatsuba
z1 = karatsuba, )
z2 = karatsuba

return + + z0