Blahhh

mail@pastecode.io avatar
unknown
python
7 months ago
867 B
1
Indexable
Never
def multiply_large_numbers(num1, num2):
    result = [0] * (len(str(num1)) + len(str(num2)))

    num1_str = str(abs(num1))
    num2_str = str(abs(num2))

    for i in range(len(num1_str) - 1, -1, -1):
        carry = 0
        n1 = int(num1_str[i])

        for j in range(len(num2_str) - 1, -1, -1):
            n2 = int(num2_str[j])
            temp_sum = (n1 * n2) + result[i + j + 1] + carry
            carry = temp_sum // 10
            result[i + j + 1] = temp_sum % 10

        result[i] += carry

    result_str = "".join(map(str, result))
    if num1 < 0 and num2 > 0 or num1 > 0 and num2 < 0:
        result_str = "-" + result_str

    return int(result_str)

# Testcase 1
result1 = multiply_large_numbers(-45952456856498465985, -98654651986546519856)
print(result1)

# Testcase 2
result2 = multiply_large_numbers(8516, -82147953548159344)
print(result2)