Problem 37 (Mortgage Calculator) no single solution?

Back to Problem Solutions forum

ahmose231     2019-01-10 11:09:13

Is it possible that problem #37 has more than one solution? In my calculation it has a range of solutions +-50. Keep up the good work :)

Rodion (admin)     2019-01-10 12:47:12
User avatar

Hm-m-m - curious question :)

Could you please give an example of input data and, say, couple of different answers which should satisfy it - I'm bit slow in understanding it without example - though it may happen you are right :)

ahmose231     2019-01-10 15:07:08

Hi :) I just ran my program for the input "500000 9 69" and submitted its output for answer, "9398". The correct answer however was 9309.

If I modify my program and calculate possible answers to get 69 months, I get a range of correct answers between 9308 and 9412.

Rodion (admin)     2019-01-14 10:03:48
User avatar

Hi again!

Please sorry for delay, I read your example and it puzzled me greatly so it took some time to recollect what I meant when writing this problem (because your objection seems just).

However after re-reading the problem statement, checker and solutions I at last figured it out (or I think so).

Really, there could be several solutions as the last payment may be less than others. However note the statement says:

should contain the required monthly payment M rounded up to whole dollars (i.e. if you get non-integer result, increase it to nearest integer which is greater)

So we want to calculate non-integer, but quite precise value of monthly payment, which allows the last payment to be equal to others. And then just round it up.

In your example with payment of 9398 the last payment is about 1376 I believe, while with 9309 the last payment is about 9233. (I think it equals to smallest integer payment allowing to pay off in given amount of months)

Radovan Markus     2020-02-03 22:10:12
User avatar

Hello again! I encountered similar problem. I calculate this m value by guessing it with help of binary search (because why not and also I don't like maths much) as that was the very first approach I could think of but the answer is, as our colleague ahmose mentioned, somewhat close to the expected result. (with examaple provided by task, my answer was 9960 and expected should be 9957 ) but for other inputs the difference was somewhat greater (ranging from like 150 to -150) I tried to change algorithm to something bit less effective, to guessing all numbers from 0 to loan but I still get only approximate answer, however I think the answer could be considered correct due to the fact that there is no limitation to the last loan, since it could be 1$ or m - 1$ and everything between should be correct as well. I asked friend about his approach and he recommended me to use binary search because that worked for him. Now I was puzzled.

Answer should contain the required monthly payment M rounded up to whole dollars (i.e. if you get non-integer result, increase it to nearest integer which is greater). So if the result is something like 9960.9 or 9960.1 it should be 9961, that doesn't make that much difference. Maybe I misunderstood something but I had to complain at least. I cannot finish this task today and I spent few hours figuring out what's wrong. I seek some kind words at least, I broke my "at least a task a day" streak and I feel now a bit dissatisfied. That's all I had to say! Thanks.

Rodion (admin)     2020-02-04 06:53:39
User avatar

Radovan, Hi!

The idea about using binary search is very good! It's quite effective when we need find solution for some long-calculating function!

I suspect there is some minor flaw in your implementation, nothing serious. Please see, I tried your solution with input 600000 17 78 - and it gives answer 12743. Let us plug this into straightforward check calculation:

M=12743
R=17
L=600000

for i in range(78):
    L = int(L * (1 + R/1200)) - M
    print(i+1, L)

https://ideone.com/nvPil0

As you see, run ends with:

76 27046
77 14686
78 2151

If I'm not mistaken, this means that while we wanted after 78 months to get rid of debt completely, with your value for M we still need one month more!

I suspect it is not the problem with your idea, but just with handling conditions and bounds. I'll try approach with binary search myself and write back...

however I think the answer could be considered correct due to the fact that there is no limitation to the last loan

Not sure what you mean :) The order of calculations described in the problem is just how banks calculate this... I used mortgage twice - they usually provide formula with description among the papers, so that people won't dare to say anything was unclear :) there is no rule like "if loan should be paid of in X months, then you can still owe small amount after X-th month" :)

Please login and solve 5 problems to be able to post at forum