我的作業問題:找出一年內最小的每月還款額。原始餘額的十二分之一是一個好的下限;一個好的上限是餘額的十二分之一,因爲每月的利息複雜一年。我的平分算法有什麼問題?
簡而言之:
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
我必須寫一個分搜索發現,最小的每月支付給分。
每次我運行這段代碼時,我都會得到最低的付款額度,這個價值是正確解決方案的幾百倍。
balance = 414866
annualInterestRate = 0.22
month = 0
monthlyinterest = (annualInterestRate)/12.0
updatedbalance = balance
monlowbound = balance/12
monupbound = (balance * (1 + monthlyinterest)**12)/12.0
mid = (monlowbound + monupbound) /2
minpay = 0
while balance > 0 and month <= 12:
balance = updatedbalance
updatedbalance = ((monthlyinterest * balance) + balance) - minpay
month += 1
if updatedbalance > 0:
minpay = (mid + monupbound)/2
mid = monlowbound
if updatedbalance < 0:
minpay = (monlowbound + mid)/2
monupbound = mid
else:
print("Lowest payment:" + " " + str(round(minpay,2)))
這是我得到的輸出:
Lowest payment: 40888.41
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
您使用的是Python 2還是3? – Lafexlos
有沒有一個原因,你不只是直接找到代數的價值? –
除非annualInterestRate實際上並不意味着它的名稱意味着什麼,否則您不正確地計算每月利率。使用「balance *(1 + annualInterestRate)== balance *(1 + monthlyInterestRate)^ 12'來計算每月利率。 –