Karatsuba algorithm

From Wikipedia, the free encyclopedia
Jump to: navigation, search

The Karatsuba algorithm is a fast multiplication algorithm. It was invented by Anatolii Alexeevitch Karatsuba in 1960 and published in 1962.[1][2][3] It reduces the multiplication of two n-digit numbers to at most 3 n^{\log_23}\approx 3 n^{1.585} single-digit multiplications in general (and exactly n^{\log_23} when n is a power of 2). It is therefore faster than the classical algorithm, which requires n2 single-digit products. If n = 210 = 1024, in particular, the exact counts are 310 = 59,049 and (210)2 = 1,048,576, respectively. The Toom–Cook algorithm is a faster generalization of this algorithm. For sufficiently large n, the Schönhage–Strassen algorithm is even faster.

The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the quadratic "grade school" algorithm.

Contents

History [edit]

The standard procedure for multiplication of two n-digit numbers requires a number of elementary operations proportional to n^2\,\!, or \Theta(n^2)\,\! in the big-O notation. In 1952, Andrey Kolmogorov conjectured that the classical algorithm was asymptotically optimal, meaning that any algorithm for that task would require \Omega(n^2)\,\! elementary operations.

In 1960, Kolmogorov organized a seminar on mathematical problems in cybernetics at the Moscow State University, where he stated the \Omega(n^2)\,\! conjecture and other problems in the complexity of computation. Within a week, Karatsuba, then a 23-year-old student, found an algorithm (later it was called "divide and conquer") that multiplies two n-digit numbers in \Theta(n^{\log_2 3}) elementary steps, thus disproving the conjecture. Kolmogorov was very agitated about the discovery; he communicated it at the next meeting of the seminar, which was then terminated. Kolmogorov published the method in 1962, in the Proceedings of the USSR Academy of Sciences. The article had been written by Kolmogorov, possibly in collaboration with Yuri Ofman, but listed "A. Karatsuba and Yu. Ofman" as the authors. Karatsuba only became aware of the paper when he received the reprints from the publisher.[2]

Algorithm [edit]

The basic step [edit]

The basic step of Karatsuba's algorithm is a formula that allows us 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.

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 = x1Bm + x0
y = y1Bm + y0,

where x0 and y0 are less than Bm. The product is then

xy = (x1Bm + x0)(y1Bm + y0)
= z2B2m + z1Bm + z0

where

z2 = x1y1
z1 = x1y0+ x0y1
z0 = x0y0.

These formulae require four multiplications, and were known to Charles Babbage.[4] Karatsuba observed that xy can be computed in only three multiplications, at the cost of a few extra additions. With z0 and z2 as before we can calculate

z1 = (x1 + x0)(y1 + y0) - z2 - z0

which holds since

z1 = x1y0+ x0y1
z1 = (x1 + x0)(y1 + y0) - x1y1 - x0y0.

Example [edit]

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

12,345 = 12 · 1000 + 345
6,789 = 6 · 1000 + 789

Only three multiplications are required, and they are operating on smaller integers are used to compute three partial results:

z2 = 12 × 6 = 72
z0 = 345 × 789 = 272,205
z1 = (12 + 345) × (6 + 789) − z2z0 = 357 × 795 − 72 − 272,205 = 283,815 − 72 − 272,205 = 11,538

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 100 like for the input operands):

result = z2 · B2m + z1 · Bm + z0, i.e.
result = 72 · 10002 + 11,538 · 1000 + 272,205 = 83,810,205.

Note that the intermediate third multiplication operates on an input domain which is less than twice 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 substractions; but note also that this partial result z1 cannot be negative: to compute these substractions, equivalent additions using complements to 10002 can also be used, keeping only the two least significant base-1000 digits for each number:

z1 = 283,815 − 72 − 272,205 = (283,815 + 999,928 + 727,795) mod 10002 = 2,011,538 mod 10002 = 11,538.

Recursive application [edit]

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 or B = 109 = 1,000,000,000, 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 1-digit long.

Efficiency analysis [edit]

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 3^{ \lceil\log_2 n \rceil} \leq 3 n^{\log_2 3}\,\!.

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

t(n) = 3 t(\lceil n/2\rceil) + cn + d

for some constants c and d. For this recurrence relation, the master theorem gives the asymptotic bound t(n) = \Theta(n^{\log_2 3})\,\!.

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.[5]

Implementation [edit]

Pseudo Code Implementation [edit]

procedure karatsuba(num1, num2)
  if (num1 < 10) or (num2 < 10)
    return num1*num2
  /* calculates the size of the numbers */
  m = max(size(num1), size(num2))
  low1, low2 = lower half of num1, num2
  high1, high2 = higher half of num1, num2
  /* 3 calls made to numbers approximately half the size */
  z0 = karatsuba(low1,low2)
  z1 = karatsuba((low1+high1),(low2+high2))
  z2 = karatsuba(high1,high2)
  return (z2*10^(m))+((z1-z2-z0)*10^(m/2))+(z0)

References [edit]

  1. ^ A. Karatsuba and Yu. Ofman (1962). "Multiplication of Many-Digital Numbers by Automatic Computers". Proceedings of the USSR Academy of Sciences 145: 293–294.  Unknown parameter |note= ignored (help)
  2. ^ a b A. A. Karatsuba (1995). "The Complexity of Computations". Proceedings of the Steklov Institute of Mathematics 211: 169–183.  Unknown parameter |note= ignored (help)
  3. ^ Knuth D.E. (1969) The art of computer programming. v.2. Addison-Wesley Publ.Co., 724 pp.
  4. ^ Charles Babbage, Chapter VIII -- Of the Analytical Engine, Larger Numbers Treated, Passages from the Life of a Philosopher, Longman Green, London, 1864; page 125.
  5. ^ [1][2]
  • Karacuba A. A.: Berechnungen und die Kompliziertheit von Beziehungen (German). Elektron. Informationsverarb. Kybernetik, 11, 603–606 (1975).

External links [edit]