【问题标题】:Recursion Error encountered in Python while writing code for Karatsuba multiplication为 Karatsuba 乘法编写代码时在 Python 中遇到递归错误
【发布时间】:2020-10-27 18:03:45
【问题描述】:

我是算法新手,我正在尝试使用递归函数调用为 Karatsuba 乘法算法编写代码。

我知道 karatsuba 乘法适用于偶数 n 位数字,方法是将它们分成两半,其中 2 个数字是 10^n/2 * a + b 和 10^n/2 * c + d 一个 X c d


通过计算得到乘积 10^n * ac + 10^n/2 * [(a+b)(c+d) - ac - b em>d] + b*d

这是我的代码,带有注释。

    def multiplication_algorithm(num1, num2):
        length1 = len(str(num1))
        length2 = len(str(num2))
        length = max(length1, length2)
        if length == 1:
            return num1 * num2 #simply returns product if single digit inputs are encountered
        num1_str = str(num1)
        num2_str = str(num2)
        num1_str = '0' * (length - length1) + num1_str #makes the length of both strings the same by adding zeros to the beginning
        num2_str = '0' * (length - length2) + num2_str
        if length % 2 != 0:
            num1_str = "0" + num1_str #makes the length of strings even so they can be symmetrically split
            num2_str = "0" + num2_str
        mid = length//2
        num1_first_half = int(num1_str[:mid]) #next 4 lines break the 2 numbers in 4 halves
        num1_second_half = int(num1_str[mid:])
        num2_first_half = int(num2_str[:mid])
        num2_second_half = int(num2_str[mid:])
        part1 = multiplication_algorithm(num1_first_half, num2_first_half)
        part3 = multiplication_algorithm(num1_second_half, num2_second_half)
        part2 = multiplication_algorithm(num1_first_half + num1_second_half, num2_first_half + num2_second_half) - part1 - part3
        return (10 ** length) * part1 + (10 ** mid) * part2 + part3

    import random
    s=set()
    for i in range(10): #generating 10 pairs of random numbers in given range to check algorithm
        number1 = random.randint(1,999)
        number2 = random.randint(1,99)
        if multiplication_algorithm(number1, number2) == number1 * number2:
            print("Success")
        else:
            print("Failure")

当我使用 random.randint(1,99) 计算的 number1 和 number2 运行此代码时,此代码运行良好,但是当我使用 number1=random.randint(1,99) 和 number2=random 运行此代码时。 randint(1,999) 如上所述,代码失败并生成递归深度错误。我已在此处复制粘贴错误文本:

    Traceback (most recent call last):
      File "C:/Users/anura/AppData/Local/Programs/Python/Python38-32/multalgo.py", line 29, in <module>
        if multiplication_algorithm(number1, number2) == number1 * number2:
      File "C:/Users/anura/AppData/Local/Programs/Python/Python38-32/multalgo.py", line 20, in multiplication_algorithm
        part3 = multiplication_algorithm(num1_second_half, num2_second_half)
      File "C:/Users/anura/AppData/Local/Programs/Python/Python38-32/multalgo.py", line 20, in multiplication_algorithm
        part3 = multiplication_algorithm(num1_second_half, num2_second_half)
      File "C:/Users/anura/AppData/Local/Programs/Python/Python38-32/multalgo.py", line 20, in multiplication_algorithm
        part3 = multiplication_algorithm(num1_second_half, num2_second_half)
      [Previous line repeated 1018 more times]
      File "C:/Users/anura/AppData/Local/Programs/Python/Python38-32/multalgo.py", line 19, in multiplication_algorithm
        part1 = multiplication_algorithm(num1_first_half, num2_first_half)
      File "C:/Users/anura/AppData/Local/Programs/Python/Python38-32/multalgo.py", line 4, in multiplication_algorithm
        length = max(length1, length2)
    RecursionError: maximum recursion depth exceeded in comparison

递归的数量远远超过它应该的数量,我不明白代码中发生了什么。

【问题讨论】:

    标签: python algorithm recursion multiplication


    【解决方案1】:

    将字符串扩展为偶数长度后,使用旧长度计算mid

    if length % 2 != 0:
        num1_str = "0" + num1_str
        num2_str = "0" + num2_str
        length += 1    # <-- this was missing
    
    mid = length//2
    

    【讨论】:

      【解决方案2】:

      至少有一个问题是 4 位数字。你把它们分成两个两位数。 num1_first_half + num2_second 可能会给出一个 3 位数的数字,然后在开头添加 0 时,您会立即回到四位数。

      维基百科页面建议在 length=4 时停止递归。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-29
        相关资源
        最近更新 更多