小编tim*_*_76的帖子

Karasuba算法,略有误差

我花了一段时间尝试在 Python 中实现 Karatsuba 的算法,虽然当我尝试将两个较大的数字(超过 ~10^15)相乘时,我的结果开始变得不准确,但我已经很接近了。我不明白为什么。

附带问题:是否有办法让我的基本情况“x 和 y 都(而不是其中之一)严格小于(而不是小于)10”


def karatsuba(x, y):
    # 1. Split ints

    if x <= 10 or y <= 10:
        #Base case
        return x * y

    n_x = ceil(log(x, 10))  # Nb of digits in x
    n_y = ceil(log(y, 10))

    n = max(n_x, n_y)

    b = int(x % (10 ** (n // 2)))
    a = int(x / (10 ** (n // 2)))
    d = int(y % (10 ** (n // 2)))
    c = int(y / …
Run Code Online (Sandbox Code Playgroud)

python algorithm karatsuba

1
推荐指数
1
解决办法
129
查看次数

标签 统计

algorithm ×1

karatsuba ×1

python ×1