使用红宝石嵌套复利

时间:2016-06-16 15:18:54

标签: ruby

p "barakas nested compound interest"

def baraka_interest(name,h,r,t,r2,r3,f)

    while  1..t do
     a = h * (1 + r)
     i = h * r  # interest on first month
     k = i*(1 + r2) # first 100 %
     m = 0.5 * k    # half of 100% profit
     v = m*(1 + r3)   # interest on 1000% profit
     z = a + m + v 
     a = z 

    end
    x = t.to_s
    y = a.to_s

  "After #{x} months #{name} will have #{y} dollars!"

end

p baraka_interest("baraka",1000,0.1,24,1,10,1)

你好,我试图计算每月10%的价值复合结果金额,但每个月后的利润也加起来为100%(k),一半的利润来自1000% (),上述计算完成24个月或2年

1 个答案:

答案 0 :(得分:0)

我不知道这段代码是否适用于您的预期(我的意思是数学)。

但这是适合你的代码

def baraka_interest(name, h, r, t, r2, r3, f)
  a = 0
  1.upto(t) do
    a += h * (1 + r)
    i = h * r # interest on first month
    k = i*(1 + r2) # first 100 %
    m = 0.5 * k # half of 100% profit
    v = m*(1 + r3) # interest on 1000% profit
    z = a + m + v
    a = z
  end
  x = t.to_s
  y = a.to_s

  "After #{x} months #{name} will have #{y} dollars!"
end

p "barakas nested compound interest"
p baraka_interest("baraka", 1000, 0.1, 24, 1, 10, 1)
# => "After 24 months baraka will have 55200.0 dollars!"