-1

My code:

balance = 320000
annualInterestRate = 0.2

originalBalance = balance
month = 1
monthly_interest = annualInterestRate / 12
low = originalBalance/12
high = (originalBalance*(1 + monthly_interest)**12)/12
epsilon = 0.01
min_payment = (high + low)/2.0

while min_payment*12 - originalBalance > epsilon:
    while month < 13:
        balance = (originalBalance - min_payment)/10 * (1+ monthly_interest)
        if balance <= 0.00:
            low = min_payment
            min_payment = (high + low)/2.0
        elif balance > 0.00:
            high = min_payment
            min_payment = (high + low)/2.0
        month += 1
print "Lowest payment: " + str(round(min_payment, 2))

However, I get 26666.0 while I'm actually supposed to get 29157.09. What am I doing wrong?

user2066771
  • 153
  • 2
  • 4
  • 11
  • balance could conceivably be exactly 0.00 at some point, which your code doesn't handle. one of those if's should be a `<=` or `>=`. – Marc B Feb 25 '13 at 22:28
  • Okay, makes sense. I changed the first one, but I still receive the same incorrect answer. Any other ideas? – user2066771 Feb 25 '13 at 22:30

3 Answers3

2

You forgot to reset month — it gets once to 13, and then the inner loop never runs again.

balance = 320000
annualInterestRate = 0.2

originalBalance = balance
month = 1
monthly_interest = annualInterestRate / 12
low = originalBalance/12
high = (originalBalance*(1 + monthly_interest)**12)/12
epsilon = 0.01
min_payment = (high + low)/2.0

while min_payment*12 - originalBalance > epsilon:
    month = 1          # < -- do this
    while month < 13:
        balance = (originalBalance - min_payment)/10 * (1+ monthly_interest)
        if balance < 0.00:
            low = min_payment
            min_payment = (high + low)/2.0
        elif balance > 0.00:
            high = min_payment
            min_payment = (high + low)/2.0
        month += 1
print "Lowest payment: " + str(round(min_payment, 2))
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
0

A mistake I think I see is that you say high = (originalBalance*(1 + monthly_interest)**12)/12. What this does is sets high to the balance times the sum of one and the interest. This is fine. However, you are taking that number and putting it to the power of 12, then divide that by 12. In your code, balance = 3000. That plus the two percent is equal to 3060. This number to the power of twelve is over 6.74 e + 41, a number I do not understand. This divided by 12 is still 5.61 e + 40. This number is so long that the program will crash(or be incredibly slow). This must be changed before anything else can possibly work.

erdekhayser
  • 6,537
  • 2
  • 37
  • 69
0

I was working on this same exercise, and here's what I noticed about your code: you wrote:

if balance <= 0.00:
  low = min_payment
  min_payment = (high + low)/2.0

However, I think that you should have set the high equal to the min_payment. If the balance is less than zero, then you've been paying too much, so your new bisection search needs to find a number lower than your last min_payment guess.

Tim Zimmermann
  • 6,132
  • 3
  • 30
  • 36
John Lutz
  • 1
  • 1