Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. monthlyInterestRate = annualInterestRate / 12
  2. lowerBound = balance / 12
  3. upperBound = (balance * (1 + annualInterestRate / 12) ** 12) / 12
  4. originalBalance = balance
  5. lowestBalance = 0.01 # Error margin e.g. $0.01
  6.  
  7. # Keep testing new payment values until the balance is +/- lowestBalance
  8. while abs(balance) > lowestBalance:
  9. # Reset the value of balance to its original value
  10. balance = originalBalance
  11. # Calculate a new monthly payment value from the bounds
  12. payment = (upperBound - lowerBound) / 2 + lowerBound
  13.  
  14. # Test if this payment value is sufficient to pay off the entire balance in 12 months
  15. for month in range(12):
  16. balance -= payment
  17. balance *= 1 + monthlyInterestRate
  18.  
  19. # Reset bounds based on the final value of balance
  20. if balance > 0:
  21. # If the balance is too big, need higher payment so we increase the lower bound
  22. lowerBound = payment
  23. else:
  24. # If the balance is too small, we need a lower payment, so we decrease the upper bound
  25. upperBound = payment
  26.  
  27. # When the while loop terminates, we know we have our answer!
  28. print("Lowest Payment: " + str(round(payment, 2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement