karatsuba乘法有大量错误

时间:2018-06-03 12:16:19

标签: python karatsuba

我编写了一个版本的python代码。基本的测试案例似乎很好,而非常大的案例karatsuba(3141592653589793238462643383279502884197169399375105820974944592,                      2718281828459045235360287471352662497757247093699959574966967627) 没有正确的答案。你可以建议哪里没有正确的代码? Env是python3。

def karatsuba(x,y):
    if x<10 and y < 10:
        return x*y
    x_len=len(str(x))
    y_len=len(str(y))
    n=max(x_len,y_len)
    m=int(n/2)

    a=int(x/(10**m))
    b=int(x%(10**m))
    c=int(y/(10**m))
    d=int(y%(10**m))

    ac=karatsuba(a,c)
    bd=karatsuba(b,d)
    sum_sum=karatsuba(a+b,c+d)
    adbc=sum_sum-ac-bd
    return int((10**(2*m))*ac+(10**m)*adbc+bd)

result=karatsuba(3141592653589793238462643383279502884197169399375105820974944592,
                 2718281828459045235360287471352662497757247093699959574966967627)
#result=karatsuba(1234,8765)
#result=karatsuba(100,23)
print(result)

0 个答案:

没有答案