Back to Problem Solutions forum
Hi i created solution using PHP, with test results i managet to get correct result, but using "99887867 9 50051923 390 7771583 984 1 2 893 774536 412 6696 583372 9 54870 3 10819 33 408329 3010817 16" I got result 6948337 end expected result was 2121504, i was fallowing example page. My code is:
// code was removed by evil admin, sorry :)
Hi! Thanks for your question!
Your code is mainly correct but suffers because of integer overflow in the intermediate results.
Remember that PHP on 32-bit machine could not properly handle integer values over ~2000000000
.
If the value becomes greater, it is converted to floating point which does not preserve least
significant digits.
You can see it for example with example input like this:
$cs = "1000000700"; // yes, just a single value, divisible by $modulator
It obviously should yield 0
, but your code gives other result.
You only need to apply % $modulator
somewhere inside the formula once more to prevent
intermediate value of $result
growing out of integer range.
Feel free to write more if the hint is not enough!
Indeed! its 32 bit, thanks! I think my code is more like spoiler now...
No worry, I will clean it up :)