Back to General discussions forum
After several failed attempts, I thought it odd that such a straightforward problem should present such apparent difficulty, so I added sections to my code that would log every relevant operation and report all of them at the end.
var n = parseInt(input());
var actions = input().split(' ');
var answer = [];
var i = 0
while(actions[0].toString() !== '%'){
n2 = n;
n = eval(n + actions[0] + parseInt(actions[1]));
answer[i] = (n2 + " " + actions[0] + " " + actions[1] + " = " + n);
i++;
actions = input().split(' ');
}
output(n % parseInt(actions[1]));
answer[i] = (n2 + " " + actions[0] + " " + actions[1] + " = " + n% parseInt(actions[1]));
alert(answer.join("\r\n"));
When I run this code and look at the output, I find that the program performs every step of the calculation correctly. This is most easily shown with your sample dataset, which will report:
5 + 3 = 8
8 * 7 = 56
56 + 10 = 66
66 * 2 = 132
132 * 3 = 396
396 + 1 = 397
396 % 11 = 1
I find the same result with larger datasets, within the limits of my ability to test (the numbers eventually get so large no calculator will handle them). My initial reaction is that this has to be a problem with the checker, but others have successfully solved the problem before. Does anyone have any ideas about this?
I forgot to mention that this is in Javascript.
You need to read the referenced "See Modular Arithmetic for thorough explanation" from the first paragraph.
Using modular arithmetic techniques will let you solve the problem without needind more than standard 32-bit integers. (Or whatever javascript uses...)
Also this may be helpful:
Topic on Fibonacci in JavaScript
Here we were discussing the same trouble - workarounds of large numbers in javascript.
>I thought it odd that such a straightforward problem should present such apparent difficulty
Well, I thought that it would be useful to make people aware of difficulty arising even with simple calculations when values do not fit in normal data types :)
>the numbers eventually get so large no calculator will handle them
You can perhaps find suitable calculator online, or, which is better, invent the test which do not require calculator:
1000
* 1000
* 1000
* 1000
* 1000
* 1000
+ 5
% 1000
Can you guess what should be the answer? And does your code return correct result? (within my browser - no).
Hope this may help! However feel free to ask for more explanations if necessary!
Dear Friends, It was one of the most interesting problems that I have seen and solved. But the reference to the topic of module algebra for this problem more confusing than helpful. The numeric data type that is needed to solve this problem is absent in Visual Basik, but it would be enough to this type of data to work with numbers ranging from -99 to +99 and the standard String. The task can be greatly simplified if you do not resort to the Built-in programming language +, *, x mod y, and replace them with equivalents (albeit rough, but totally understandable). Just think of how these operations can be calculated manually on paper and implement the same in your programming language.
Just do modulo after each step. The result will be the same. My solution - http://www.codeabbey.com/index/task_solution?task=modular-calculator&user=vadim-tukaev&lang=Java
i can't clear this section coz whenever i input the data that given random by code abbey, the result just around 2 digits but the correct answer is more than 2 digits
The calculation is likely to exceed uint64, so you need to use modular arithmetic.