我的作业问题:找出一年内支付给定贷款本金所需的最小月付款。原始余额的十二分之一是良好的下限;一个好的上限是余额的十二分之一,在其利息每月复利一年后。
简而言之:
old_app
我必须写一个二分搜索来找到每分钱最小的一分钱。
每次运行此代码时,我都会从最正确的解决方案中获得最低的付款价值几百美元。
old_app
0020_auto_others
0021_custom_rename_models.py
dependencies:
('old_app', '0020_auto_others'),
('app_x', '0002_auto_20170608_1452'),
('app_y', '0005_auto_20170608_1452'),
('new_app', '0001_initial'),
0022_auto_maybe_empty_operations.py
dependencies:
('old_app', '0021_custom_rename_models'),
0023_custom_clean_models.py
dependencies:
('old_app', '0022_auto_maybe_empty_operations'),
app_x
0001_initial.py
0002_auto_20170608_1452.py
0003_update_fk_state_operations.py
dependencies
('app_x', '0002_auto_20170608_1452'),
('old_app', '0021_custom_rename_models'),
app_y
0004_auto_others_that_could_use_old_refs.py
0005_auto_20170608_1452.py
0006_update_fk_state_operations.py
dependencies
('app_y', '0005_auto_20170608_1452'),
('old_app', '0021_custom_rename_models'),
这是我得到的结果:
Monthly interest rate = (Annual interest rate) / 12.0
Monthly payment lower bound = Balance / 12
Monthly payment upper bound = (Balance * (1 + Monthly interest rate)**12) / 12.0
答案 0 :(得分:1)
主要问题是您每月应用反馈调整逻辑(调整每月付款)。您需要等到年底,然后调整付款。所有这些都应该包含在一个while
循环内,这个循环一直持续到你足够接近" ...比如,在前一笔付款的一分钱内。像这样:
last_pay = -1000 # Ridiculous value to start the process
while abs(last_pay - minpay) > 0.01:
# use your current logic for paying off one year, including
...
for month in range(12):
....
# HERE is where you check the final balance
# to see whether you're high or low.
# Then adjust your monthly payment (minpay)
这会让你前进吗?
答案 1 :(得分:0)
您的算法不起作用。
你想要的是:
你似乎同时做两件事,即你改变你的" mid"你仍在计算的价值。我建议你在尝试编码之前记下你的算法,以实现你可能想要的流程:
def finalbalance(fixedpayment): #code确定平衡状态
def solvebisection(lowbound,highbound,function = finalbalance,tofind = 0): #Recursive编码到"找到根"