CPI 的递归函数
Recursive function for CPI
尝试更好地理解递归。我想制作一个函数,显示给定起始金额的特定年份的 CPI 增长。
假设起始金额为100000,CPI率为5%,则f(0) = 100000, f(1) = 5000, f(2) = 5250 etc.I 想要return 下面的 CPIincrease 栏
rate 0.05
n TotalCPI CPIincrease
0 100000
1 105000 5000
2 110250 5250
3 115762.5 5512.5
4 121550.625 5788.125
5 127628.1563 6077.53125
6 134009.5641 6381.407812
7 140710.0423 6700.478203
8 147745.5444 7035.502113
9 155132.8216 7387.277219
10 162889.4627 7756.64108
到目前为止,我从上面的列中得到了 TotalCPI table
def CPIincreases(n):
if n<=0:
return initial
else:
return (CPIincreases(n-1))*(1+CPIrate)
initial = 100000
CPIrate = 0.05
print(CPIincreases(1),CPIincreases(2),CPIincreases(3),CPIincreases(4))
output: 105000.0 110250.0 115762.5 121550.625
现在我迷路了。因为输出显示我应该添加
CPIincrease(n) - CPIincrease(n-1)
某处。
非常感谢任何帮助,即使它说这个功能是不可能的。
干杯
您创建的函数计算随时间推移的总金额,正如您所指出的,您可以调用它两次(一次使用 year
,一次使用 year - 1
)并取差来得到你的答案。
如果你真的想一次递归地做这件事,我们需要考虑基本情况:
- 第0年:一开始没有兴趣
return 0
- 第 1 年:第一年后的变化只是初始金额乘以利率
return initial * rate
- 第 2 年+:从今年开始,我们的收入与去年相同,再加上该利息的利息
return last_year + rate * last_year
- 或者只是:
return last_year * (1 + rate)
现在我们可以把它们放在一起了:
def cpi_increase(year, initial, rate):
if year == 0:
return 0
if year == 1:
return initial * rate
return (1 + rate) * cpi_increase(year - 1, initial, rate)
如果我们打印出来,我们可以看到匹配值:
initial = 100000
rate = 0.05
for year in range(11):
print('{year:<5} {total:<21} {cpi_increase}'.format(
year=year,
total=initial * (1 + rate) ** year,
cpi_increase=cpi_increase(year, initial, rate)
))
值:
0 100000.0 0
1 105000.0 5000.0
2 110250.0 5250.0
3 115762.50000000001 5512.5
4 121550.62500000003 5788.125
5 127628.15625000003 6077.53125
6 134009.56406250005 6381.407812500001
7 140710.04226562506 6700.478203125001
8 147745.5443789063 7035.502113281251
9 155132.8215978516 7387.2772189453135
10 162889.4626777442 7756.64107989258
思考我们的基本案例还展示了如何创建直接计算。在 y
年,我们应用了 (1 + rate)
乘法 y - 1
次和基数 (initial * rate)
一次。这给了我们:
def cpi_increase_direct(year, initial, rate):
if year <= 0:
return 0
return initial * rate * (1 + rate) ** (year - 1)
我喜欢 Jon 的回答更加详尽。这是我的代码,我试图让变量名不言自明,但我也会简要描述它们。
total_cpi: first column
cpi_increase: 2nd column
cpi_rate: CPIrate
如果我们需要用一个递归函数来解决这个问题,我们只能使用状态变量来解决这个问题:-
def calculate_cpi_increase(total_cpi, cpi_increase, year):
if year == 0:
return total_cpi, cpi_increase
else:
return calculate_cpi_increase(total_cpi*(1+cpi_rate), total_cpi*cpi_rate, year-1)
cpi_rate = 0.05
calculate_cpi_increase(100000, 0, 10)
结果:(162889.46267774416, 7756.641079892579)
首先,您不应该使用要打印的所有值来调用递归函数。例如,如果您调用 F(2)
、F(3)
、F(4)
和 F(5)
,您将重复 F(2)
的计算 4 次,因为每个其他调用都需要此计算。
你也不应该使用全局变量,你可以使用我的简单方法并将它们封装在另一个函数中。在这段代码中,我不仅生成了一个值,还生成了完整的 table,一个 python 元组的列表。任何元组都是两个值,值和每次迭代的增量。完整的 table 由另一个函数打印。
def CPITable(n, initial = 100000, CPIrate = 0.05):
def CPITableRecurse(n):
if n<=0:
return [(initial,0)]
else:
CPI = CPITable(n-1)
inc = CPI[-1][0] * CPIrate
CPI.append((CPI[-1][0] + inc , inc ))
return CPI
return CPITableRecurse(n)
def printTable(table):
i = 0
for line in table:
print ( str(i) + " %5.2f %5.2f" % line)
i += 1
printTable(CPITable(6))
#output:
# 0 100000.00 0.00
# 1 105000.00 5000.00
# 2 110250.00 5250.00
# 3 115762.50 5512.50
# 4 121550.62 5788.12
# 5 127628.16 6077.53
# 6 134009.56 6381.41