Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Karatsuba算法-我的实现中的错误_Python_Algorithm_Multiplication - Fatal编程技术网

Python Karatsuba算法-我的实现中的错误

Python Karatsuba算法-我的实现中的错误,python,algorithm,multiplication,Python,Algorithm,Multiplication,我在用python实现karatsuba算法时遇到困难。我正在处理base 2中的列表(MSB位于列表的末尾)。给我的实施是: Input: 2 bit-numbers of bit length n Output: their product a*b function Karatsuba(a, b) if(n == 1) return a*b else a1, a0, leftmost(n/2)(rounded up), rightmost(n/2

我在用python实现karatsuba算法时遇到困难。我正在处理base 2中的列表(MSB位于列表的末尾)。给我的实施是:

Input: 2 bit-numbers of bit length n
Output: their product a*b

function Karatsuba(a, b) 
     if(n == 1) return a*b
     else 
          a1, a0, leftmost(n/2)(rounded up), rightmost(n/2)(rounded down) bits of a
          b1, b0, leftmost(n/2)(rounded up), rightmost(n/2)(rounded down) bits of b

     s1 = Karatsuba(a1, b1)
     s2 = Karatsuba(a0, b0)
     s3 = Karatsuba(a1 + a0, b1 + b0)

     return s1 * 2^n + (s3 - s1 - s2) * 2^(n/2) + s2
这是我的python实现:

def karatsuba(A, B):
    if(len(A) == 1 or len(B) == 1):
        return Multiply(A, B)

    n = max(len(A), len(B))
    m = n / 2

    print "Karatsuba call"

    print "A", A, "\n"
    print "B", B, "\n"

    lowA = A[:m]
    highA = A[m:]

    lowB = B[:m]
    highB = B[m:]

    print "highA", highA, "\n"
    print "lowA", lowA, "\n"

    print "highB", highB, "\n"
    print "lowB", lowB, "\n"

    s1 = karatsuba(highA, highB)
    s2 = karatsuba(lowA, lowB)
    s3 = karatsuba(Add(highA, lowA), Add(highB, lowB))

    f1 = Multiply(s1, pow2(n))
    f2 = Multiply(Sub(Sub(s3, s1), s2), pow2(m))
    return Add(f1, Add(f2, s2))
但是,使用输入运行(请记住MSB是最右边的位):

我得到了
产品Karatsuba[0,0,0,1,0,0,0,1,0]72
,但它应该输出
[0,0,1,0,0,1]36
。函数Add、Substract、pow2和Multiply正在工作,我分别对它们进行了测试。如果有帮助,下面是打印语句的完整输出:

Karatsuba call
A [0, 1, 1] 

B [0, 1, 1] 

highA [1, 1] 

lowA [0] 

highB [1, 1] 

lowB [0] 

Karatsuba call
A [1, 1] 

B [1, 1] 

highA [1] 

lowA [1] 

highB [1] 

lowB [1] 

Karatsuba call
A [0, 1] 

B [0, 1] 

highA [1] 

lowA [0] 

highB [1] 

lowB [0] 

Karatsuba call
A [1, 1] 

B [1, 1] 

highA [1] 

lowA [1] 

highB [1] 

lowB [1] 

Karatsuba call
A [0, 1] 

B [0, 1] 

highA [1] 

lowA [0] 

highB [1] 

lowB [0] 
我搜索了几个小时,我不知道我的错误在哪里。有人能帮我吗?谢谢

错误如下:

f1 = Multiply(s1, pow2(n))
应该是:

f1 = Multiply(s1, pow2(2*m))
实际上,
(a1*2^m+a0)*(b1*2^m+b0)=(a1*b1)*2^(2m)+(a0*b1+a1*b0)*2^m+(a0*b0)


如果
n>(2*m)
,这是一个奇数n,那么您做的事情不正确…

@greybeard对不起,我不确定我是否明白您的意思?您确定您的算术函数给出了正确的结果吗?如果是这样,那么最终结果的MSB中怎么会有零呢?它不应该总是1吗(除非值为零)?@samgak是的,它们是给我的;我只是添加了实现pow2n,这是正确的(如果您需要,我可以展示它)。我不知道为什么有这个额外的0。是的,请张贴它。在我看来,您的打印语句输出是正确的,因此我怀疑这不是您发布的代码。另外,如果将Add(f1,Add(f2,s2))赋值给一个变量,并在从函数返回之前将其打印出来,您会得到什么日志?@samgak我试图用钢笔和铅笔以及算法描述来计算它。我得到的最终结果是9*2^3,实际上是72。因此,要么我误解了算法,要么我给出的描述中有错误(这会很奇怪,因为这是我的课程幻灯片)。可能吗?哦!非常感谢!我猜我在我的论文上执行算法时也犯了同样的错误!现在它起作用了:)澄清一下;伪代码有点误导,对吗?它说它应该是s1*2^n,但实际上它是s1^2*(n/2),由于整数除法,它不会产生相同的结果,对吗?我想我可以做出同样的假设,但显然我错了……事后想想,这听起来像是
f1 = Multiply(s1, pow2(2*m))