基本ruby递归:堆栈级别太深(SystemStackError)



我想这是由于无限递归。我做错了什么?我的目标是找到能被1-20整除的最小数。

def smallest(n, count)
  (2..n).each do |num|
    if count % num != 0
      count += 1
      smallest(n, count)
    end
  end
  return count 
end
puts smallest(20, count=20)

您确定在这个例子中需要递归吗?在不调用smallest(n, count)的情况下尝试您的代码。

顺便说一句,这个算法找到了不能被1-20整除的数字。

相关内容

  • 没有找到相关文章

最新更新