[Task 35] Savings Calculator wrong result ?

Back to Problem Solutions forum

flitzpiepe     2015-04-09 07:33:00

Hi,

In the input data for this task there was the tuple (50, 800, 1) for which the requested solution apparently is 280.

My solution is 279, though. And I don't see an error in the calculations. All the other tuples from the input values where correct and my solution worked on a different set of input data, so my solution is already uploaded. What's going on there ?

flitzpiepe     2015-04-09 07:41:20

EDIT:

when testing it with this (python) code, I also get the result 279 for the #iterations:

v = 50.0
c = 0
while v <= 800.0:
    v = round(v+v*0.01,2)
    c += 1
print v
print c
Quandray     2015-04-09 10:22:10
User avatar

Python is not my first language, but I think the problem is because you are using floating point variables.

If you do the calculation using integers, i.e. use cents instead of dollars and cents, you'll get an answer of 280. Try this

v = 5000
c = 0
p = 1
while v < 80000:
    v += int(v*p/100)
    c += 1
print v
print c
flitzpiepe     2015-04-09 13:51:20

So the original computation did not actually round any values but rather cut off everything after the 2nd digit after the decimal point, didn't it ? IMO, this would be wrong. Unless I am missing some other detail here.

E.g. the second step starts at 5050 (cents), +1% this is 5100.5 (cents), rounded this should make 5101 (cents). But with the integer method it makes only 5100. These rounding issues will then add up to a full year of additional waiting ;)

Quandray     2015-04-09 16:59:59
User avatar

Ah, sorry my mistake, it's the rounding that is causing your problem. The problem description says "At the end of each year account is increased and rounded down to whole cents".

That's banks for you!

flitzpiepe     2015-04-10 05:31:15

You are right, I missed the part about rounding down Thanks for pointing it out

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