Last active
December 16, 2015 06:29
-
-
Save PatchRowcester/5392207 to your computer and use it in GitHub Desktop.
Python program that will calculate the minimum monthly payment to finish paying a loan in 12 months, using bisection search.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
balance = float(raw_input('Enter the balance: ')) | |
annualInterestRate = float(raw_input('Enter the annual interest rate (decimal): ')) | |
##defining the variables | |
monthlyInterestRate = annualInterestRate/12.0 | |
##Lowest payment will not be lower than balance/12 | |
low = balance/12.0 | |
##highest payment not will not be higher than the total amount payable at the end of the year | |
high = (balance*(1 + monthlyInterestRate)**12)/12.0 | |
epsilon = 0.01 | |
initialPayment = (high + low)/2.0 | |
while True: | |
##store balance in a temporary variable | |
tempBalance = balance | |
for i in range(1,13): | |
monthlyUnpaidBalance = tempBalance - initialPayment | |
tempBalance = monthlyUnpaidBalance * (1 + monthlyInterestRate) | |
if (abs(round(tempBalance,2)))<=epsilon: | |
break | |
elif (round(tempBalance,2))>epsilon: | |
low = initialPayment | |
else: | |
high = initialPayment | |
initialPayment = (high + low)/2.0 | |
print('Result your code should generate:') | |
print('-------------------') | |
print('Lowest Payment: ' + str(round(initialPayment,2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment