I am getting this error:
integerMultiplication.rb:4:in `untMul': stack level too deep (SystemStackError)
in this code:
def untMul(x, y)
xDigits = x.to_s.split(//).map{|chr| chr.to_i}
yDigits = y.to_s.split(//).map{|chr| chr.to_i}
n = xDigits.size
a = xDigits[0, xDigits.size / 2]
b = xDigits[xDigits.size / 2 ... xDigits.size]
c = yDigits[0, yDigits.size / 2]
d = yDigits[yDigits.size / 2 ... yDigits.size]
ac = untMul(a,c)
bd = untMul(b,d)
adPlusBd = untMul(a + b, c + d) - ac - bd
return 10**n * ac + 10**n/2 * adPlusBd + bd
end
untMul(12, 54)
Could someone please help with understand what is going wrong here? I am trying to implement Karatsuba multiplication.
Infinite recursion is going on. untMul is calling itself repeatedly until the stack runs out of space. You need to set a condition case to make it complete before that happens.