Karazuba algorithm

from Wikipedia, the free encyclopedia

The Karazuba algorithm is an algorithm for multiplying two large whole numbers . It was developed in 1960 by the 23-year-old Anatoly Alexejewitsch Karazuba (English Karatsuba, Russian Анатолий Алексеевич Карацуба ) and published in 1962.

If the number of bits of the two numbers and a Landau symbol designate , the algorithm is significantly faster than the school method with a runtime complexity of . This (and also its implicit transfer to the binary system in the form of the Russian pawn multiplication ) has a runtime complexity of . Karazuba's method became the model for the divide-and-conquer principle in computer science. For sufficiently large numbers, the Karazuba algorithm is slower than its generalizations, such as the Toom-Cook algorithm (1965) and the Schönhage-Strassen algorithm (1971), whose runtime complexity is and which, from the point of view of complexity theory, is the fastest algorithm for multiplication large until 2007, when Martin Fürer presented a further development with a (previously only theoretically) lower runtime complexity.

Idea of ​​the algorithm

In the school method, multiplication causes an effort that grows with the square of the number of digits, while additions and shift operations , in which one multiplies by a power of the base of the place value system used, only require linear effort. The idea is to split the two numbers to be multiplied into two parts according to the divide-and-conquer principle, and to replace the multiplications with additions and shift operations as far as possible. The multiplication of the divided numbers results in three sub-terms, which are formed by four multiplications. These can be combined to form the overall result by shifting and adding operations. One of these terms is a sum of two products. This term can be written as the difference with a new product and the sum of the other two sub-terms. Overall, you save a partial multiplication. If you carry out this process recursively , you get a significantly more favorable running time than with the school method.

The algorithm in detail

The algorithm given here applies to natural numbers, but it can easily be generalized to whole numbers by taking their signs into account separately. The factors and are represented as tuples in the place value system . The value of is irrelevant: For example, in a computer with a multiplier for 32-bit wide numbers would be chosen. The examples below use decimal numbers . To recursion to be able to perform, the lengths of both Zifferntupel be a power of two with , and it was . This can always be achieved by using suitable leading zeros; This does not change anything essential in the run-time estimate carried out below.

The number tuples are therefore

and

Each digit tuple is now split into two length tuples . That gives the four numbers

and as well
and

So is

and

When multiplied it results

The term can now be put into another form that can be calculated more quickly:

This results in the representation for the product

in which only the three "short" products

appear. They are calculated recursively and linked with simple shift and addition operations

Of the four possible products (of X with Y) X h Y h , X h Y l , X l Y h , X l Y l , the first P 1 and the last P 2 are used directly in the result. The term from the sum of the two middle products can be formed as the sum of all products minus the first and last product. The sum of all four products can be generated using the newly introduced product P 3 with just one multiplication.

Runtime analysis

A multiplication of two -digit numbers is traced back to three multiplications of two -digit numbers and four additions or subtractions of -digit numbers, possibly with carryovers and with two shifts. The time required for operations that are not multiplications is less than with one of the independent constants . Designates the total number of operations when multiplying two -digit numbers, then applies

The first case of the Master's theorem that can be used here and provides the runtime complexity of The direct derivation with complete induction enables a more precise insight:

Replacing with results in

Product forming example

Let the numbers to be multiplied be

and .

Since this is only about the illustration of the product reshaping, it is filled with leading zeros to the next even and equal length and not to a two-power length. This results in the number tuples

and

of length , which are divided into four tuples of length :

and as well
and

It applies

and

The products needed are

,
and

The algorithm would determine the products , and recursively. It remains to compose the result according to the above formula:

While the school method requires 110 digit multiplications and 90 additions (without carry-overs), here there are 92 multiplications and 83 additions.

generalization

Instead of dividing the numbers into two parts, the numbers to be multiplied can also be broken down into more parts. With a clever linear combination of partial results, when decomposing into parts, multiplications on the smaller numbers are sufficient . Applied recursively, this procedure then leads to the Toom Cook algorithm .

literature

  • A. Karatsuba, Y. Ofman: Multiplication of Many-Digital Numbers by Automatic Computers. In: Soviet Physics-Doklady. 7, 1963, pp. 595-596 (English translation of the Russian original. In: Doklady Akad. Nauk SSSR. Volume 145, 1962, pp. 293-294).
  • AA Karacuba: Calculations and the Complexity of Relationships. In: electron. Information processing Cybernetics. 11, 1975, pp. 603-606.
  • AA Karatsuba: The Complexity of Computations. In: Proc. Steklov Inst. Math. 211, 1995, pp. 169-183 ( cs.ru PDF; English translation of the Russian original. In: Trudy Mat. Inst. Steklova. 211, 1995, pp. 186-202).
  • DE Knuth: The Art of Computer Programming. Volume 2: Seminumerical Algorithms. Addison-Wesley Publ. Co., Reading, Mass., 1969, ISBN 0-201-89684-2 .

Web links

References and comments

  1. The English Lemma Fürer's algorithm contains some information on this.