|
|
Line 1: |
Line 1: |
| The '''Karatsuba algorithm''' is a fast [[multiplication algorithm]]. It was discovered by [[Anatolii Alexeevitch Karatsuba]] in 1960 and published in 1962.<ref name="kara1962">
| | Hi there, I am Alyson Boon even though it is not the name on my beginning certification. I am presently a journey agent. The favorite pastime for him and his children is to perform lacross and he would never give it up. Ohio is where his home is and his family loves it.<br><br>my website ... [http://fashionlinked.com/index.php?do=/profile-13453/info/ free psychic] |
| {{cite journal
| |
| | author = A. Karatsuba and Yu. Ofman
| |
| | title = Multiplication of Many-Digital Numbers by Automatic Computers
| |
| | journal = Proceedings of the USSR Academy of Sciences
| |
| | volume = 145
| |
| | year = 1962
| |
| | pages = 293–294
| |
| | postscript = . Translation in the academic journal ''[[Physics-Doklady]]'', '''7''' (1963), pp. 595–596}}
| |
| </ref><ref name="kara1995">
| |
| {{cite journal
| |
| | author = A. A. Karatsuba
| |
| | title = The Complexity of Computations
| |
| | journal = Proceedings of the Steklov Institute of Mathematics
| |
| | volume = 211
| |
| | pages =169–183
| |
| | year = 1995
| |
| | url = http://www.ccas.ru/personal/karatsuba/divcen.pdf
| |
| | postscript = . Translation from Trudy Mat. Inst. Steklova, 211, 186–202 (1995)}}
| |
| </ref><ref name="knuthV2">
| |
| Knuth D.E. (1969) ''The art of computer programming. v.2.'' Addison-Wesley Publ.Co., 724 pp.</ref> It reduces the multiplication of two ''n''-digit numbers to at most <math>3 n^{\log_23}\approx 3 n^{1.585}</math> single-digit multiplications in general (and exactly <math>n^{\log_23}</math> when ''n'' is a power of 2). It is therefore faster than the [[long multiplication|classical]] algorithm, which requires ''n''<sup>2</sup> single-digit products. If ''n'' = 2<sup>10</sup> = 1024, in particular, the exact counts are 3<sup>10</sup> = 59,049 and (2<sup>10</sup>)<sup>2</sup> = 1,048,576, respectively.
| |
| The [[Toom–Cook multiplication|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.
| |
| | |
| == History ==
| |
| The standard procedure for multiplication of two ''n''-digit numbers requires a number of elementary operations proportional to <math>n^2\,\!</math>, or <math>\Theta(n^2)\,\!</math> 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 <math>\Omega(n^2)\,\!</math> elementary operations.
| |
| | |
| In 1960, Kolmogorov organized a seminar on mathematical problems in [[cybernetics]] at the [[Moscow State University]], where he stated the <math>\Omega(n^2)\,\!</math> conjecture and other problems in the [[computational complexity|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 <math>\Theta(n^{\log_2 3})</math> 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 Petrovich Ofman|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.<ref name="kara1995"/>
| |
| | |
| ==Algorithm==
| |
| ===The basic step===
| |
| 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 [[radix|base]] ''B''. For any positive integer ''m'' less than ''n'', one can write the two given numbers as
| |
| | |
| :''x'' = ''x''<sub>1</sub>''B''<sup>''m''</sup> + ''x''<sub>0</sub>
| |
| :''y'' = ''y''<sub>1</sub>''B''<sup>''m''</sup> + ''y''<sub>0</sub>,
| |
| | |
| where ''x''<sub>0</sub> and ''y''<sub>0</sub> are less than ''B''<sup>''m''</sup>. The product is then
| |
| | |
| :''xy'' = (''x''<sub>1</sub>''B''<sup>''m''</sup> + ''x''<sub>0</sub>)(''y''<sub>1</sub>''B''<sup>''m''</sup> + ''y''<sub>0</sub>)
| |
| ::= ''z''<sub>2</sub>''B''<sup>''2m''</sup> + ''z''<sub>1</sub>''B''<sup>''m''</sup> + ''z''<sub>0</sub>
| |
| | |
| where
| |
| | |
| :''z''<sub>2</sub> = ''x''<sub>1</sub>''y''<sub>1</sub>
| |
| :''z''<sub>1</sub> = ''x''<sub>1</sub>''y''<sub>0</sub>+ ''x''<sub>0</sub>''y''<sub>1</sub>
| |
| :''z''<sub>0</sub> = ''x''<sub>0</sub>''y''<sub>0</sub>.
| |
| | |
| These formulae require four multiplications, and were known to [[Charles Babbage]].<ref>Charles Babbage, Chapter VIII – Of the Analytical Engine, Larger Numbers Treated, [http://books.google.com/books?id=Fa1JAAAAMAAJ&pg=PA125 Passages from the Life of a Philosopher], Longman Green, London, 1864; page 125.</ref> Karatsuba observed that ''xy'' can be computed in only three multiplications, at the cost of a few extra additions. With ''z''<sub>0</sub> and ''z''<sub>2</sub> as before we can calculate
| |
| | |
| :''z''<sub>1</sub> = (''x''<sub>1</sub> + ''x''<sub>0</sub>)(''y''<sub>1</sub> + ''y''<sub>0</sub>) – ''z''<sub>2</sub> – ''z''<sub>0</sub>
| |
| which holds since
| |
| | |
| :''z''<sub>1</sub> = ''x''<sub>1</sub>''y''<sub>0</sub>+ ''x''<sub>0</sub>''y''<sub>1</sub>
| |
| :''z''<sub>1</sub> = (''x''<sub>1</sub> + ''x''<sub>0</sub>)(''y''<sub>1</sub> + ''y''<sub>0</sub>) – ''x''<sub>1</sub>''y''<sub>1</sub> – ''x''<sub>0</sub>''y''<sub>0</sub>.
| |
| | |
| ===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 (''B''<sup>''m''</sup> = ''1000''), as:
| |
| : 12345 = '''12''' · ''1000'' + '''345'''
| |
| : 6789 = '''6''' · ''1000'' + '''789'''
| |
| | |
| Only three multiplications are required, and they are operating on smaller integers are used to compute three partial results:
| |
| : ''z''<sub>2</sub> = '''12''' '''×''' '''6''' = 72
| |
| : ''z''<sub>0</sub> = '''345''' '''×''' '''789''' = 272205
| |
| : ''z''<sub>1</sub> = ('''12''' + '''345''') '''×''' ('''6''' + '''789''') − ''z''<sub>2</sub> − ''z''<sub>0</sub> = 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 = ''z''<sub>2</sub> · ''B''<sup>2''m''</sup> + ''z''<sub>1</sub> · ''B''<sup>''m''</sup> + ''z''<sub>0</sub>, i.e.
| |
| : result = 72 · ''1000''<sup>2</sup> + 11538 · ''1000'' + 272205 = '''83810205'''.
| |
| | |
| 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 subtractions; but note also that this partial result ''z''<sub>1</sub> cannot be negative: to compute these subtractions, equivalent additions using complements to ''1000''<sup>2</sup> can also be used, keeping only the two least significant base-''1000'' digits for each number:
| |
| : ''z''<sub>1</sub> = 283815 − 72 − 272205 = (283815 + 999928 + 727795) '''mod''' ''1000''<sup>2</sup> = 2011538 '''mod''' ''1000''<sup>2</sup> = 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 [[recursion|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 [[Multiplication ALU|multiplier]], for example, one could choose ''B'' = 2<sup>31</sup> = 2,147,483,648 or ''B'' = 10<sup>9</sup> = 1,000,000,000, and store each digit as a separate 32-bit binary word. Then the sums ''x''<sub>1</sub> + ''x''<sub>0</sub> and ''y''<sub>1</sub> + ''y''<sub>0</sub> 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==
| |
| 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 2<sup>''k''</sup>, for some integer ''k'', and the recursion stops only when ''n'' is 1, then the number of single-digit multiplications is 3<sup>''k''</sup>, which is ''n''<sup>''c''</sup> where ''c'' = log<sub>2</sub>3.
| |
| | |
| 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 <math>3^{ \lceil\log_2 n \rceil} \leq 3 n^{\log_2 3}\,\!</math>.
| |
| | |
| 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
| |
| | |
| :<math>t(n) = 3 t(\lceil n/2\rceil) + cn + d</math>
| |
| | |
| for some constants ''c'' and ''d''. For this [[recurrence relation]], the [[master theorem]] gives the [[big O notation|asymptotic]] bound <math>t(n) = \Theta(n^{\log_2 3})\,\!</math>.
| |
| | |
| 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.<ref>[http://gmplib.org/manual/Karatsuba-Multiplication.html][http://ozark.hendrix.edu/~burch/proj/karat/comment1.html]</ref>
| |
| | |
| ==Implementation==
| |
| ===Pseudo Code Implementation===
| |
| <source lang="pli">
| |
| procedure karatsuba(num1, num2)
| |
| if (num1 < 10) or (num2 < 10)
| |
| return num1*num2
| |
| /* calculates the size of the numbers */
| |
| m = max(size_base10(num1), size_base10(num2))
| |
| m2 = m/2
| |
| /* split the digit sequences about the middle */
| |
| low1, high1 = split_at(num1, m2)
| |
| low2, high2 = split_at(num2, m2)
| |
| /* 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^(2*m2))+((z1-z2-z0)*10^(m2))+(z0)
| |
| </source> | |
| | |
| ==References==
| |
| {{Reflist}}
| |
| * Karacuba A. A.: ''Berechnungen und die Kompliziertheit von Beziehungen ([[German language|German]]).'' Elektron. Informationsverarb. Kybernetik, 11, 603–606 (1975).
| |
| | |
| ==External links==
| |
| * [http://www.cs.pitt.edu/~kirk/cs1501/animations/Karatsuba.html Karatsuba's Algorithm for Polynomial Multiplication]
| |
| *{{MathWorld|urlname=KaratsubaMultiplication|title=Karatsuba Multiplication}}
| |
| * [http://utilitymill.com/utility/Karatsuba_Multiplication Karatsuba multiplication Algorithm – Web Based Calculator (GPL)]
| |
| * Bernstein, D. J., "[http://cr.yp.to/papers/m3.pdf Multidigit multiplication for mathematicians]". Covers Karatsuba and many other multiplication algorithms.
| |
| * [http://www.ccas.ru/personal/karatsuba/divcen.htm Karatsuba Multiplication on Fast Algorithms and the FEE]
| |
| * [https://github.com/yaojingguo/cpp-code/blob/master/kmul.cc Karatsuba multiplication in C++]
| |
| | |
| | |
| {{Number-theoretic algorithms}}
| |
| | |
| [[Category:Computer arithmetic algorithms]]
| |
| [[Category:Multiplication]]
| |