【发布时间】:2012-03-22 19:48:31
【问题描述】:
我收到此错误:
integerMultiplication.rb:4:in `untMul': stack level too deep (SystemStackError)
在这段代码中:
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)
有人可以帮助了解这里出了什么问题吗?我正在尝试实现 Karatsuba 乘法。
【问题讨论】:
-
顺便说一句,如果您在谷歌上搜索过该错误消息,您会发现一连串的答案和问题引导您了解正在发生的事情。
标签: ruby recursion integer multiplication