Multiply two integers without using multiplication, division, bitwise operators, and loops
Oct 10, 2024
Claude 3.5 Sonnet couldn’t produce bug-free code for this simple problem, especially for large inputs, so I decided to write my own :-)

Let me share my solution:
def get_mul(a, b, result, power_of_2):
if a == 0 or b == 0:
return 0
elif a == 1:
return b
elif a < 0:
return -get_mul(-a, b, result, power_of_2)
elif b < a:
return get_mul(b, a, a, 1)
elif a < power_of_2 + power_of_2:
return get_mul(a, b, b, 1)
return result + get_mul(a - power_of_2, b, result + result, power_of_2 + power_of_2)
a, b = 1234567890909090, -12189892121212
assert a * b == get_mul(a, b, b, 1)
a, b = -197761, 901
assert a * b == get_mul(a, b, b, 1)