17 problem?

Back to Problem Solutions forum

Vadimas S     2015-11-16 17:35:55

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 :)
Rodion (admin)     2015-11-16 20:09:11
User avatar

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!

Vadimas S     2015-11-16 20:20:22

Indeed! its 32 bit, thanks! I think my code is more like spoiler now...

Rodion (admin)     2015-11-16 20:29:44
User avatar

No worry, I will clean it up :)

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