Multiplication algorithm: Difference between revisions

Content deleted Content added
Scovetta (talk | contribs)
m added Java as supporting bignums
No edit summary
Line 1:
A '''multiplication algorithm''' is an [[algorithm]] (or method) to [[multiplication|multiply]] two numbers. Depending on the size of the numbers, different algorithms are in use.
 
A major advantage of [[numeral system|positional numeral system]]s over other systems of writing down numbers is that they facilitate the usual grade-school method of '''long multiplication''': multiply the first number with every digit of the second number and then add up all the properly shifted results. In order to perform this algorithm, one needs to know the products of all possible digits, which is why [[multiplication table]]s have to be memorized. Humans use this algorithm in base 10, while computers employ the same algorithm in base 2. The algorithm is a lot simpler in base 2, since the multiplication table has only 4 entries. Rather than first computing the products, and then adding them all together in a second phase, computers add the products to the result as soon as they are computed. Modern chips implement this algorithm for 32-[[bit]] or 64-[[bit]] numbers in [[hardware]] or in [[microcode]].microcodewith To multiply two''m''-digit numbers with ''nx'' digits using this method<sub>1</sub>, one needs about ''nx''<supsub>2</supsub> operations. More formally: the time complexity of multiplying two, ''ny''-digit<sub>1</sub> numbersand using long multiplication is [[Big O notation|&Omicron;]](''ny''<supsub>2</supsub>). The product is given by
 
An old method for multiplication, that does not require multiplication tables, is the [[Peasant multiplication]] algorithm; this is actually a method of multiplication using base 2.
 
For systems that need to multiply huge numbers in the range of several thousand digits, such as [[computer algebra system]]s and [[bignum]] libraries, this algorithm is too slow.
These systems employ '''Karatsuba multiplication''' which was discovered in 1962 and proceeds as follows: suppose you work in base 10 (unlike most computer implementations) and want to multiply two ''n''-digit numbers ''x'' and ''y'', where ''n'' is even and equal to 2''m'' (if ''n'' is odd instead, or the numbers are not of the same length, this can be corrected by adding zeros at the left end of ''x'' and/or ''y''). We can write
: ''x'' = ''x''<sub>1</sub> 10<sup>''m''</sup> + ''x''<sub>2</sub>
: ''y'' = ''y''<sub>1</sub> 10<sup>''m''</sup> + ''y''<sub>2</sub>
with ''m''-digit numbers ''x''<sub>1</sub>, ''x''<sub>2</sub>, ''y''<sub>1</sub> and ''y''<sub>2</sub>. The product is given by
: ''xy'' = ''x''<sub>1</sub>''y''<sub>1</sub> 10<sup>2''m''</sup> + (''x''<sub>1</sub>''y''<sub>2</sub> + ''x''<sub>2</sub>''y''<sub>1</sub>) 10<sup>''m''</sup> + ''x''<sub>2</sub>''y''<sub>2</sub>
so we need to quickly determine the numbers ''x''<sub>1</sub>''y''<sub>1</sub>, ''x''<sub>1</sub>''y''<sub>2</sub> + ''x''<sub>2</sub>''y''<sub>1</sub> and ''x''<sub>2</sub>''y''<sub>2</sub>. The heart of Karatsuba's method lies in the observation that this can be done with only three rather than four multiplications:
Line 53 ⟶ 45:
 
[[Category:Arithmetic]][[Category:Algorithms]]
lalalalalalalalalalalalalalalalalalalalalalalalala