## mensana 2 years ago Someone managed to solve the problem 3 set 2: Using Bisection Search to Make the Program Faster. I need help to understand this problem.

1. RCMorea

Do you mean problem 3 of set 1? Because the first set was number 0? That is the one I recall that needed to go faster by binomial search. If so, I just finished it. If you did the previous one, then your loops should pretty much work the same (mine did). Next, you have to find an upperbound and lowerbound to test against, which the problem set tells you how to do, so that part is pretty easy. Next, you set your payment halfway between the upperbound and lowerbound: payment = (upperbound+lowerbound)/2. After that, this was my logic: 1) if remaining balance after 12 months is too high, raise your payment. 2) if remaining balance after 12 months is too low, lower your payment. 3) if remaining balance after 12 months is correct, then stop. To raise your payment, set payment = (payment+upperbound)/2. REMEMBER to also set your new lowerbound to equal your old payment. Actually, it's easier to do it in reverse order. Set lowerbound = payment, then raise payment. To lower payment, do the opposite. To recognize when you are done, basically since you are going in 1 cent increments and everything has to be the same, you could overpay by up to 11 cents. So, that is your range -11 to 0 for being done. I think that's right. I got the same answers as the professor within 2 cents, which I think is probably rounding and I'm not worried about it. If you want, I can post code.

2. anirudh796

paste the code

3. mensana

I wish!

4. RCMorea

OK, here is my code. It works (gives the right answer). You might not like it because I used a function. The same basic logic should work without a function, I just thought the function was easier that way. Also, of course, the professor posted his own solution in a .zip file in the lesson 4 materials. http://pastebin.com/e5UiBiVE

5. mensana

Thanks RCMorea! I tested your code, but the results are different tests. Could you post the link of the file where I can download the solution of the teacher.

6. RCMorea

Interesting. It's giving me 29643.04 for the first payment. I believe the "book answer" is 29643.05, but that is a rounding error. Also, it is giving me 91679.91 as the second payment, which is right on the book answer. The final balances I am getting -.03 for the first one (book answer -.1) and -.06 for the second one (book answer -.12) but again that is rounding, and I think mine is actually more accurate than the professors (he rounds before the math but I round after). If anyone else checks this, I would be curious what you get. But...I will say the professors code is a better example to look at anyway (no surprise, of course).

7. bwCA

which course (2008 or 2011)? which ps? which problem?

8. RCMorea

this was course 2011 (well, at least that is the one i did, maybe others did a different one that could account for some differences), second problem set (but numbered ps1), last problem (the one where you use bisecgtion search).

9. bwCA

the problem set and lecture order is different for the 2008 and 2011sc series

10. bwCA

if it is the problem for paying off debt then: - initialize the lower limits of your bisection search to 0 and the upper limit to the original balance. - make a guess at the halfway point - run the guess through the payment cycle - if the guess was too big, set the upper limit to the guess and repeat - if the guess was tool small, set the lower limit to the guess and repeat

11. bwCA
12. mensana

Thank bwCa and RcMorea for your help.