Algorithm(1) - Karatsuba multiplication

这个系列主要是记一下目前效率较高或者比较出名的一些算法.算法

Karatsuba multiplication:spa

x=5678   then: a=56  b=67orm

y=1234           c=12 d=34ip

setps: it

1:   a*c = 672    ①io

2:   b*d=2652   ②class

3:  (a+b)(c+d)=6164  ③效率

4:  ③-②-①=2840call

5:  6720000 + 2652+284000 = 7006652计算机

Recursive algorithm:

whrite: x= 10n/2 a+b   y= 10 n/2 c+d

then x*y = 10nac+10n/2(ad+bc)+bd   这里,咱们须要作4次乘法,在计算机中的cost并不理想,因此用到一个

Gauss's trick:

step1: recursively compute ac

step2: recurisively compute bd

step3: recurisively compute (a+c)*(c+d)  then

ad+bc = (a+c)*(c+d) - ac - bd

upshot:only 3 recursive multiply calls. 

note: 这里的n表示位数, 好比x是6位数,n=6, n/2=3,若是x=7,则n/2取4.

保留一个问题,这个是我比较困惑的, 若是x和y位数相差比较大这个算法还能不能用, 好比x是7位数,y是三位数,但愿大神解答!

在计算机里,少作一次乘法的效率会提升很多,对于给定的n位大数,算法的复杂度不超过3nlog3 ≈ 3n1.585, 通常给定N位数,复杂度是n平方。

相关文章
相关标签/搜索