python实现五大基本算法_Python的大数运算到底是根据什么基础原理或者算法实现的?...

Naive (grade school) multiplication has a running time of O(n^2).

Python uses Karatsuba multiplication whose running time is O(n^1.585).

Karatsuba Multiplication Algorithm

Also, a combination of Karatsuba, Toom-Cook, and Nussbaumer convolution can be used to get a running time of O(n*ln(n)).

======================

Python 为什么使用“复杂度更高”的Karatsuba Multiplication而不是大家常念叨的O(nlogn)的FFT大法?

(1)

In practice the Schönhage–Strassen algorithm starts to outperform

older methods such as Karatsuba and Toom–Cook multiplication for

numbers beyond 2**2**15 to 2**2**17 (10,000 to 40,000 decimal digits). [1](2) 讲道理的话,python使用者用的“大数”哪里会超过10000位。。。

[1] Schönhage

=======================

Karatsuba Multiplication的源码?

yes, talk is cheap, plz search it (python) on github. [2]

其实可以看到,当其中任意一个数的位数很少的时候,python是用朴素乘法的。。。

这说明朴素乘法在该情况下更快。。

[2] cpython/longobject.c at master · python/cpython · GitHub

=======================

Karatsuba Multiplication什么时候加进python的?

2002年 [3]

[3] Issue 560379: Karatsuba multiplication

=======================

既然当位数很少的时候,Python用更快的朴素乘法,那为什么不加一个判断条件,当位数超级多的时候用FFT这种方法?

I'm far from sure that such a patch would be accepted. :-) Indeed,

Tim Peters has been heard to mention that if he were to do it all

again, he probably wouldn't even have implemented Karatsuba [4].

Asymptotically fast integer multiplication is a specialist need that's

already available in 3rd-party Python libraries (gmpy). IMO, it's not

appropriate to include it in a general-purpose programming language,

and the burden of maintaining such code would outweigh the benefits.

One thing that has been considered in the past is making it possible

to use GMP directly for Python's implementation of long integers; see

Victor Stinner's efforts in this direction [5]. Licensing concerns,

and the fact that Python's implementation is faster for small

integers, ended up killing this issue.

[4] [Python-Dev] Optionally using GMP to implement long if available

[5] Issue 1814: Victor Stinner's GMP patch for longs

=======================

Karatsuba算法的思想?复杂度怎么算的。。

分治,其实贴的链接里面提得挺多的了。。下面简单描述一下。

要算

equation?tex=x%5Ctimes+y,假设它们都是n位数。。把x和y都拆成2个n/2的部分

equation?tex=x%3D%5Coverline%7Bab%7D%2C+y%3D%5Coverline%7Bcd%7D

普通的算法是计算

equation?tex=a%5Ctimes+c%2C+a%5Ctimes+d%2Cb%5Ctimes+c%2Cb%5Ctimes+d,这样1次size为n的大数乘法 变成 4次size为n/2的大数乘法加上1次size为n/2的大数加法,这样得到的复杂度递推式是

equation?tex=T%28n%29%3D4T%28n%2F2%29%2B%5CTheta%28n%2F2%29,然后解这个递推式子得到复杂度

equation?tex=%5CTheta%28n%5E2%29

然而,其实我们只要求

equation?tex=a%5Ctimes+d%2Bb%5Ctimes+c而不必分别求它们,而且我们发现

equation?tex=b%5Ctimes+c%2Ba%5Ctimes+d%3D%28a%2Bb%29%5Ctimes%28c%2Bd%29-a%5Ctimes+c-b%5Ctimes+d,这样1次size为n的大数乘法就变成了 3次size为n/2的大数乘法加上3次size为n/2的大数加法(减法也当作加法),这样得到的复杂度递推式为

equation?tex=T%28n%29%3D3T%28n%2F2%29%2B3%5CTheta%28n%2F2%29,使用主方法解这个递推式得到复杂度

equation?tex=%5CTheta%28n%5E%7Blog_23%7D%29

========================

a combination of Karatsuba, Toom-Cook, and Nussbaumer convolution是什么?怎么combine?

不知道。这句话是GMP [6]的作者写的。有兴趣可以去看GMP的实现。:)

[6] The GNU MP Bignum Library

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值