Karatsuba algorithm: Difference between revisions

Content deleted Content added
Enkya (talk | contribs)
Algorithm: Renamed the terms so that the lowest digit is 0 not 2; renamed X,Y to Z2,Z0; reworded analysis.
Line 4:
The speed of this algorithm relies partly on the ability to do [[arithmetic shift|shift]]s faster than a standard multiply, with shifts typically taking [[linear time]] on a practical computer.
 
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.
We assume two ''n''-digit numbers ''x'' and ''y'', represented in [[radix|base]] ''B'' where ''B'' is a convenient value to shift with, like two in modern binary computers. Choosing a number ''m'' less than ''n'', we can write
 
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 split the two given numbers as follows
:''x'' = ''x''<sub>1</sub>''B''<sup>''m''</sup> + ''x''<sub>2</sub>
:''y'' = ''y''<sub>1</sub>''B''<sup>''m''</sup> + ''y''<sub>2</sub>
 
where :''x''<sub>2</sub> and= ''yx''<sub>21</sub> are less than ''B''<sup>''m''</sup>; it+ is easily seen that there is a unique representation. We now have''x''<sub>0</sub>
:''xy'' = ''xy''<sub>1</sub>''B''<sup>''m''</sup> + ''xy''<sub>20</sub>
 
:''xy'' =where (''x''<sub>10</sub>''B''<sup>''m''</sup> +and ''x''<sub>2</sub>)(''y''<sub>10</sub> are less than ''B''<sup>''m''</sup>. +The ''y''<sub>2</sub>)product is then
::= ''x''<sub>1</sub>''y''<sub>1</sub>''B''<sup>2''m''</sup> + (''x''<sub>1</sub>''y''<sub>2</sub> + ''x''<sub>2</sub>''y''<sub>1</sub>)''B''<sup>''m''</sup> + ''x''<sub>2</sub>''y''<sub>2</sub>
 
:''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>)
The standard method is to multiply the four subproducts separately, then finally shift and add; this is O(n<sup>2</sup>). However, Karatsuba observed that we can compute ''xy'' in only three multiplications:
::= ''z''<sub>2</sub> ''B''<sup>2''m''</sup> + ''z''<sub>1</sub> ''B''<sup>''m''</sup> + ''z''<sub>0</sub>
 
where
:Let ''X'' = ''x''<sub>1</sub>''y''<sub>1</sub>
:Let ''Y'' = ''x''<sub>2</sub>''y''<sub>2</sub>
:Let ''Z'' = (''x''<sub>1</sub> + ''x''<sub>2</sub>)(''y''<sub>1</sub> + ''y''<sub>2</sub>) - X - Y
 
:Let ''Yz''<sub>2</sub> = ''x''<sub>21</sub>''y''<sub>21</sub>
We note that
::''z''<sub>1</sub> = ''x''<sub>1</sub>''y''<sub>20</sub> + ''x''<sub>20</sub>''y''<sub>1</sub>
:''z''<sub>0</sub> = ''x''<sub>0</sub>''y''<sub>0</sub>
 
These formulas require four multiplications. Karatsuba observed that we can compute ''xy'' in only three multiplications, at the cost of a few extra additions:
:''Z'' = (''x''<sub>1</sub>''y''<sub>1</sub> + ''x''<sub>1</sub>''y''<sub>2</sub> + ''x''<sub>2</sub>''y''<sub>1</sub> + ''x''<sub>2</sub>''y''<sub>2</sub>) - ''x''<sub>1</sub>''y''<sub>1</sub> - ''x''<sub>2</sub>''y''<sub>2</sub>
::= ''x''<sub>1</sub>''y''<sub>2</sub> + ''x''<sub>2</sub>''y''<sub>1</sub>
 
:''y'' =Let ''yz''<sub>12</sub> = ''Bx''<supsub>''m''1</supsub> + ''y''<sub>21</sub>
Thus ''xy'' = ''X'' ''B''<sup>2m</sup> + ''Y'' + ''Z'' ''B''<sup>m</sup>; we have cut down the number of multiplications from four to three.
:Let ''Xz''<sub>0</sub> = ''x''<sub>10</sub>''y''<sub>10</sub>
:''Z''Let = (''x''<sub>1</sub>''yz''<sub>1</sub> += (''x''<sub>1</sub>''y''<sub>2</sub> + ''x''<sub>20</sub>)(''y''<sub>1</sub> + ''x''<sub>2</sub>''y''<sub>20</sub>) - ''x''z<sub>12</sub>''y''<sub>1</sub> - ''x''z<sub>20</sub>''y''<sub>2</sub>
 
since
To compute these three products of ''m''-digit numbers, we can [[recursion|recursively]] employ Karatsuba multiplication again. The shifts, additions and subtractions take linear time respectively, and can be considered negligible.
 
::''z''<sub>1</sub> = (''x''<sub>1</sub>''y''<sub>1</sub> + ''Bx''<supsub>1</sub>2''my''<sub>0</supsub> + (''x''<sub>10</sub>''y''<sub>21</sub> + ''x''<sub>20</sub>''y''<sub>10</sub>) - ''Bx''<supsub>1</sub>''my''<sub>1</supsub> +- ''x''<sub>20</sub>''y''<sub>20</sub>
Karatsuba multiplication works best when the two operands are of similar size; thus while one can theoretically choose ''m'' arbitrarily, the best time bound would be achieved by choosing ''m'' such that the subproducts are roughly equal, that is setting ''m'' to be about ''n''/2.
:Let ''Z'' := (''x''<sub>1</sub> + ''xy''<sub>20</sub>)( + ''yx''<sub>10</sub> + ''y''<sub>21</sub>) - X - Y
 
To compute these three products of ''m''-digit numbers, we can [[recursion|recursively]] employ Karatsuba multiplication again, until the numbers become so small that their product can be computed directly. One typically chooses ''B'' so that this condition is verified when the numbers are 1 digit each. In a computer with a full 32-bit by 32-bit [[multiplier circuit|multiplier]], for example, one could choose ''B'' = 2<sup>32</sup> or ''B'' = 10<sup>9</sup>, and store each digit as a separate 32-bit binary word.
 
Karatsuba's basic step works for any base ''B'' and any ''m'', but the recursive algorithm is most efficient when, and ''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 recursive Karatsuba algorithm performs at most 3<sup>''k''</sup> multiplications of 1-digit numbers; that is, ''n''<sup>''c''</sup> where ''c'' = log<sub>2</sub>3. When ''n'' is large, Karatsuba's algorithm is much faster than the grade school method, which requires ''n''<sup>2</sup> single-digit multiplications. If ''n'' = 2<sup>10</sup> = 1024, for example, it requires 3<sup>10</sup> 59,049 instead of (2<sup>10</sup>)<sup>2</sup> = 1,048,576 single-digit multiplies.
 
Since the additions, subtractions, and digit shifts (multiplications by powers of ''B'') in Karasuba's basic step take time proportional to ''n'', their cost of all becomes negligible as ''n'' increases.
 
==Example==