def karatsuba(x: int, y: int) -> int: """Multiply 2 same lenght ints using the karatsuba algorithm""" # base case if x < 10 or y < 10: return x * y # Calculate the number of digits of the larger number n = max(len(str(x)), len(str(y))) n_2 = n // 2 # split in half a, b = divmod(x, 10**n_2) c, d = divmod(y, 10**n_2) # Recursively compute bd = karatsuba(b, d) pq = karatsuba((b + a), (d + c)) ac = karatsuba(a, c) # Combine return (ac * 10 ** (2 * n_2)) + ((pq - ac - bd) * 10**n_2) + bd # test x = 9823892839835678 y = 1239823892839834 result = karatsuba(x, y) print(f"{x} x {y} = {result}")