假设一个人每月向银行存入一个语料库。银行每月付给他利息。
现在对于给定的投资主体p,利率r和,任期t,
我想计算任期结束后的总数。
如果你google这个术语的复合能力,你可以找到成千上万的在线计算器来解决这个问题。
举个例子(注:这不同于简单的复利问题):
If P is 52.5, R is 3.6% Anually, T is 5 Months, Suppose Total Amount is A
Now, R will be 0.3% monthly (since bank pays him every month)
After First Month: A = 52.5*(1 + 0.003) = 52.6575 [His Corpus with added Interest]
**Then he Deposit Corpus again**
After Second Month: A = (52.5 + 52.6575)*(1 + 0.003) = 105.4729725
After Third Month: A = (52.5 + 105.4729725)*(1 + 0.003) = 158.4468914
After Forth Month: A = (52.5 + 158.4468914)*(1 + 0.003) = 211.5797321
After Fifth Month: A = (52.5 + 211.5797321)*(1 + 0.003) = 264.8719713 ~ 265
所以,答案是265。
这个问题的一个简化可能是这样的:(Python 2)
p=float(raw_input('Enter the Corpus: '))
r=(1+float(raw_input('Enter the Annual Rate: '))/1200)
t=input('Enter the Tenure: ')
a=p+0
m=1
while m<t:
a=(a+p)*r
m+=1
print 'Total Amount',int(round(a))
我知道这不是最好的办法我花了几个小时试图推导出这个问题的一般公式,但失败了。所以,我不知道这个问题是否存在任何公式或任何更好的算法。
最佳答案
我们可以找到解决以下问题的封闭式解决方案(与您所说的略有不同,但解决方案将以任何一种方式工作):
a(n+1) = a(n) * r + p
a(0) = A
a(1) = A*r + p
a(2) = a(1)*r + p
a(2) = (A*r + p)*r + p
a(2) = A*r^2 + p*r + p
a(3) = a(2)*r + p
a(3) = (A*r^2 + p*r + p)*r + p
a(3) = A*r^3 + p*r^2 + p*r + p
...
这个和是一个几何级数,你可以通过谷歌搜索一下,发现它简化为:
这可以在o(1)中用o(1)
pow
函数来计算幂。关于algorithm - 复利的力量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38488331/