Baraka Letara - 1 year ago 59

Ruby Question

`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)

hello i am trying to calculate what the result amount will be for value compounded on monthly basis at 10% , but the profit after each month is also compounded at 100% (k) , and half of the profit from the 1000%(), the above calculation is done for 24 months or 2 yrs

Answer

I dont know if this code works as you expectid (i mean the math).

But this is valid code for you

```
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!"
```

Source (Stackoverflow)