Problem with task 14 modular calculator

Back to General discussions forum

kristaps1000     2019-11-30 13:25:33

Hello, I can't figure out, why my code doesn't work with the problem's input, because with smaller inputs it seems to work just fine. I am using Java, and this is my code is in comments

kristaps1000     2019-11-30 13:26:47

nvm, I simply had to use BigInteger

vimal_singh     2019-12-02 11:26:53

Well I don't see your code.

But still I can give you a hint, you can divide the numbers after each operation and use the remainder for next operation. This will keep the result from overflowing to the negative side.

Sandeep Makwana     2020-01-11 05:59:33

first find term and all values are %term by using array int term = y[stepcount-1]; for(int i = 0; i < stepcount; i++) { if(op[i] == '*'){x = x * y[i] % term;} if(op[i] == '+'){x = (x + y[i]) % term;} } x = x % term;

Rodion (admin)     2020-01-11 20:19:25
User avatar

nvm, I simply had to use BigInteger

that's not correct :)

BigInteger will work here, but fail in further similar problems, due to long calculation. The same with default endless int in Python.

Look at this problem for example.

You had to read the "Modular Arithmetic" article referenced in the problem statement instead - then your soultion would be similar to what colleagues explained above - and without heavy calculations.

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