Ala carte final
unknown
python
2 years ago
1.4 kB
9
Indexable
def ala_carte_multiply(num1, num2):
# Convert the input numbers to strings to work with individual digits
num1_str = str(num1)
num2_str = str(num2)
# Initialize an array to store intermediate results
intermediate_results = []
# Initialize a variable to keep track of the number of zeros to append
num_zeros_to_append = 0
# Iterate through each digit of num2 in reverse order
for digit2 in reversed(num2_str):
digit2 = int(digit2)
carry = 0
temp_result = ["0"] * num_zeros_to_append # Append zeros for place value
# Multiply num1 by digit2
for digit1 in reversed(num1_str):
digit1 = int(digit1)
product = digit1 * digit2 + carry
temp_result.append(str(product % 10))
carry = product // 10
# If there's a carry after multiplying with all digits in num1, append it
if carry > 0:
temp_result.append(str(carry))
# Reverse the temp_result and join it to form a number
intermediate_results.append(int("".join(reversed(temp_result))))
# Increment the number of zeros to append for the next iteration
num_zeros_to_append += 1
# Sum up all the intermediate results
final_result = sum(intermediate_results)
return final_result
# Example usage:
num1 = 123456789
num2 = 987654321
result = ala_carte_multiply(num1, num2)
print(result)
Editor is loading...