-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL4ProblemSet2.py
85 lines (67 loc) · 2.47 KB
/
L4ProblemSet2.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Test Case 1:
balance = 4213
annualInterestRate = 0.2
monthlyPaymentRate = 0.04
# Test Case 2:
# balance = 4842
annualInterestRate = 0.2
monthlyPaymentRate = 0.04
# ================================
# Code to pay only minimum payment
# ================================
totalPaid = 0
remainingBalance = balance
unpaidBalance = balance
monthlyInterestRate = annualInterestRate / 12.0
for i in range(1, 13):
minimumMonthlyPayment = monthlyPaymentRate * remainingBalance
unpaidBalance = remainingBalance - minimumMonthlyPayment
remainingBalance = unpaidBalance * (1 + monthlyInterestRate)
totalPaid += minimumMonthlyPayment
print('Month: %u' % i)
print('Minimum monthly payment: %.2f' % minimumMonthlyPayment)
print('Remaining balance: %.2f' % remainingBalance)
print('Total paid: %.2f' % totalPaid)
print('Remaining balance: %.2f' % remainingBalance)
print('=======')
# ================================
# Code to pay in 12 mos. to the nearest $10
# ================================
balance = 3926
annualInterestRate = 0.2
monthlyPayment = 0 # initialize
remainingBalance = balance
unpaidBalance = balance
monthlyInterestRate = annualInterestRate / 12.0
while remainingBalance > 0:
monthlyPayment += 10
remainingBalance = balance
for i in range(1, 13):
unpaidBalance = remainingBalance - monthlyPayment
remainingBalance = unpaidBalance * (1 + monthlyInterestRate)
print('Lowest Payment: %u' % monthlyPayment)
print('=======')
# ================================
# Code to pay in 12 mos. using bisection
# ================================
balance = 999999
annualInterestRate = 0.18
monthlyPayment = 0 # initialize
remainingBalance = balance
unpaidBalance = balance
monthlyInterestRate = annualInterestRate / 12.0
monthlyPaymentLB = balance / 12
monthlyPaymentUB = (balance * (1 + monthlyInterestRate) ** 12) / 12.0
while monthlyPaymentUB - monthlyPaymentLB > 0.01:
monthlyPayment = (monthlyPaymentLB + monthlyPaymentUB) / 2.0
remainingBalance = balance
for i in range(1, 13):
unpaidBalance = remainingBalance - monthlyPayment
remainingBalance = unpaidBalance * (1 + monthlyInterestRate)
print('Remaining Balance with a monthly payment of %.2f: %.2f' % (monthlyPayment, remainingBalance))
if remainingBalance > 0:
monthlyPaymentLB = monthlyPayment
elif remainingBalance < 0:
monthlyPaymentUB =monthlyPayment
monthlyPayment = round(monthlyPayment, ndigits=2)
print('Lowest Payment: %.2f' % monthlyPayment)