Back to General discussions forum
I came across something a little strange while answering Problem 35: Savings Calculator.
One of my answers showed up as incorrect.
Starting amount: 25
Required sum: 450
Interest rate: 2%
My program gave the result as 146 years, but the required result was shown as 147 years. I checked my code, but couldn't see a problem so I checked an online savings calculator site, which agreed my result of 146 years. As a test, a resubmitted my code without any changes, but with a new set of data, and this time it went through as correct. Obviously not a major problem, but just thought I'd let you know.
Stephen, Hi! Thanks for your message!
I think this little discrepancy is because your solution does not perform this operation:
>At the end of each year ... rounded down to whole cents (as in example above).
As we see it only becomes important when the term is long and percents are small...
Aargh! "Read the question!" - gets me everytime!
Thanks for that, Rodion. Rounding down to whole cents does indeed give 147 years. I went back and submitted a corrected solution.
I have a question about this same problem. My solution sometimes gives the right answers and sometimes makes a mistake of one year. Should I be concerned and try to make it better or could it be just an error of rounding or something?
P.S. The errors happen very rarelly.
Hi,
Does your code round down to whole cents? This bit looks like it may round up
if ((int)temp%10 >= 5) {
temp += 10;
}
Oh wow. As English is not my main language I wouldn't have noticed the word down. I fixed my code, Thank You.
Hi! I got the corrected solution. however, when I run the example, the last two lines are 29 9316.79 30 10062.13 while your answer is 29 9316.82 30 10062.16 the other are same.
Then I find the problem(int(5033.61 * 100) = 503360), how I solve the problem?
Sorry, I can't help you with python, but if
int(5033.61 * 100) = 503360
What is
5033.61 * 100
From the Python2 documentation:
int(x=0):
Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.
int(x, base=10):
If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.
Now, as for the question of whether int(5033.61 * 100) = 503360
, Python 2.7.2 does not agree with this assertion. In fact, it creates an integer object of value 503361.
Furthermore, 5033.61 * 100
returns a value of 503361.00000000006. (Why is this happening?)
So, to summarize, for all Python versions (through 2.7.11 and 3.5):
Decimal.quantize()
method.