ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey All,
I am working on a MITx class and having problems with the following, I am supposed to use bisection search to find the min payment of the given balance over the course of year for it to be 0. Been working on this the last twoish days and have not gotten stuck.
Its outputting that the Lowest Payment is 314574.88. From the answer I see on the site it should be 29157.09. I have a feeling I am missing something obivios here. Any one have any ideas?
There are a lot of errors in your program, I noted them in red:
Code:
balance = 320000
annualInterestRate = 0.2 # not an error, but 20% interest rate, yikes!
monInterest=annualInterestRate/12
originalBalance=balance
low=originalBalance/12
epsilon = 0.01 # your epsilon is too big
high = (originalBalance*(1 + monInterest)**12)/12
min_payment = (high+low)/2
total_interest=0
print "Before While: " + str(min_payment*12 - (originalBalance + total_interest) )
# should be using epsilon here
while min_payment*12 - (originalBalance + total_interest) >= 0:
print "Total Inter: "+ str(total_interest)
print "Min Paymen: " + str(min_payment)
# you need to reset balance and total_interest here
for month in range(0, 11): # a year has 12 months
balance = balance - min_payment * ( monInterest) # interest doesn't apply to the payment
print "Bal in For: " + str(balance)
total_interest += (balance * monInterest)
# you need to compound your interest
#print total_interest
print "Bal " + str(balance)
# you have your bisectioning backwards
if balance < 0:
low = min_payment
elif balance > 0:
high = min_payment
min_payment=(high + low)/2.0 # this should not be exclusive to the left bisection
# you printed the wrong variable here
print "Lowest Payment: " + str(round(balance,2))
@joshp, I fixed up your code (there are errors in your code that haven't been listed yet) and the correct answer is $29,643.04. The $29,057.09 that you see on the site is the amount owing at the beginning of the 12th month but interest still has to be added for that month too.
I verified my answer using a spreadsheet program and this is what the spreadsheet displays:
If you can't get your code working properly, PM me and I will send you my listing. (It's actually written in blassic but the syntax is similar to python).
Not really a gotcha, unless one is new to Python. It's documented (see help(range) in Python's interactive mode or pydoc range in the shell) and consistent to how list slicing works: foo[n:m] will return list elements from n (inclusive) to m (exclusive).
# should be using epsilon here
while min_payment*12 - (originalBalance + total_interest) >= 0:
You should also use an absolute value function.
Yes, I was trying to leave a bit of thought for the OP to do.
Quote:
Code:
for month in range(0, 11): # a year has 12 months
Counting from 0 to 11 gives 12 months so that is not the problem.
As audriusk mentioned, python's range is right-open, so range(0, 11) means [0, 11).
Quote:
Sounds like a gotcha to me. That's why I prefer the for syntax used in C.
It would be even more of a gotcha if you had to do range(0, len(list)-1) to get the indexes of a list. Also note that range(12) is the same as range(0, 12).
Quote:
The correct formula for calculating the balance is: balance = balance * (1 + monInterest) - min_payment
...
the correct answer is $29,643.04. The $29,057.09 that you see on the site is the amount owing at the beginning of the 12th month but interest still has to be added for that month too.
$29,643.04 is the answer if you make payments at the end of the month, $29,157.09 if you make them at the beginning.
Quote:
If you can't get your code working properly, PM me and I will send you my listing. (It's actually written in blassic but the syntax is similar to python).
They won't be able to use PMs until 150 posts. I would also ask you to refrain from handing out answers.
$29,643.04 is the answer if you make payments at the end of the month, $29,157.09 if you make them at the beginning.
If the payments are made at the beginning of the month then my formula for the balance is incorrect. Also, the loan would effectively be paid off in 11 months instead of 12 so the exit condition of the outer loop as written in the OP is also incorrect (of course there was a much simpler test that could have been used).
Quote:
Originally Posted by ntubski
They won't be able to use PMs until 150 posts.
I was unaware of that. It seems unnecessarily restrictive to me.
Quote:
Originally Posted by ntubski
I would also ask you to refrain from handing out answers.
In my experience (I have been a private tutor for many years), it will never happen.
Sounds like a gotcha to me. That's why I prefer the for syntax used in C.
It's actually pretty much like the syntax in C if you are used to using less-than. range(x, y) maps to for (i = x; i < y; ++i) and range(x, y, z) maps to for (i = x; i < y; i += z).
If the payments are made at the beginning of the month then my formula for the balance is incorrect. Also, the loan would effectively be paid off in 11 months instead of 12 so the exit condition of the outer loop as written in the OP is also incorrect (of course there was a much simpler test that could have been used).
The time between the first and last payments is 11 months in both cases, but there are 12 payments in both cases so the outer loop test is still correct (except for the abs() and epsilon thing).
The time between the first and last payments is 11 months in both cases, but there are 12 payments in both cases so the outer loop test is still correct (except for the abs() and epsilon thing).
The outer loop test does not give the balance as you would expect because total_interest is not calculated correctly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.