forked from ssantic/edXPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpset2prob3.py
More file actions
27 lines (22 loc) · 971 Bytes
/
pset2prob3.py
File metadata and controls
27 lines (22 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'''
This program calculates the fixed minimum monthly payment needed to pay off
the complete credit card balance within one year. It uses the bisection search
(also known as biserial search) algorithm, and then prints out the value.
'''
balance = 999999
annualInterestRate = 0.18
monthlyInterestRate = annualInterestRate/12
lowerBound = balance/12 #Lower bound for bisection search
upperBound = (balance * (1 + monthlyInterestRate)**12)/12 #Upper bound
minMonthlyPayment = (lowerBound + upperBound)/2.0 #Startin minimum payment
newBalance = balance
while abs(newBalance) > 0.01:
newBalance = balance
for month in range (1,13):
newBalance = (newBalance - minMonthlyPayment) * (1 + monthlyInterestRate)
if newBalance <= 0: #This loop is charachteristic for bisection search
upperBound = minMonthlyPayment
else:
lowerBound = minMonthlyPayment
minMonthlyPayment = (lowerBound + upperBound)/2.0
print ("Lowest payment: " + str(round(minMonthlyPayment,2)))