Solution for prob 14 Modular calculator It strictly seems me that there is a bug on the server

Back to Problem Solutions forum

shatseli     2020-01-16 17:07:05

Hi Admin Please check ON YOUR SERVER side the script for problem #14 ... I checked some times my source with a input values and it does looks like bug is ont the server:

public class Modular_Calculator
 {

    public static void main(String[] args)
    {
        Scanner lineScanner = new Scanner(System.in); 
        double result  = lineScanner.nextInt();
        lineScanner.nextLine(); // skip this DataRow?

        while (lineScanner.hasNextLine())
        { 
            String DataRow= lineScanner.nextLine();
            if (DataRow.isEmpty())
            { 
                System.out.print("result ="+ result );
                break;
            }

            String[] cells = DataRow.split(" ");

            int value = Integer.parseInt(cells[1]);

            char ch =cells[0].charAt(0); 
            switch (ch)
            {
                case '+':
                    result += value  ;
                    break ;
                case '-':
                    result -= value  ;
                    break ;
                case '*':
                    result *= value  ;
                    break ;
                case '%':
                    result %= value  ;
                    System.out.print("case % result ="+ result );
                    break ;
                default :
                    value = 0;
                    break;
            }  
            System.out.print(" value ( result) = "+ value +"("+ result + ")" );
        }
        System.out.print("finally result ="+ result );
    }
}
Rodion (admin)     2020-01-16 18:18:37
User avatar

Hey Friend!

Our server is all right, problem was solved over 4000 times by people :)

Check your code with this input

10
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
* 10000000
% 100

Obviously result should be 0, but you most probably get different value. E.g. ideone gives "final result=84.0"...

Your solution works only for short calculations due to loss of precision. :(

P.S. read the article on "Modular Arithmetic" (mentioned in the problem statement) thorougly please. Or check other forum posts on this problem (feel free to use search in the header).

shatseli     2020-01-17 16:29:07

Dear Rodion, Thank you for your quick and detailed answer !

But ...considering from the very beginning that the problem of lost precision may be caused by size of variable I defined the accumulating variable "result" as double type to use fully all it's 64 positions. (you may see it in the source ) ( and considering the values in records I leaved the size of variable "value" still remained as "int" ) so the precision should be equal to size of variable result, isn't it ?

Or I wrongly understanding smth. ?

Rodion (admin)     2020-01-17 16:37:08
User avatar

Dear Eli, Hi!

so the precision should be equal to size of variable result, isn't it ?

That's all correct. Just 64 bit isn't enough. If you check the example above (or the data generated by server), it is easy to see that multiplying values about 1000 just 7 times will exhaust 64 bit value.

Just add to your code something like

double maxPreciseDouble = 1e15; // roughly
if (result > maxPreciseDouble) {
    System.out.println("ALARM!");
}

To preserve the precision you just need to apply modulo operation after every multiplication or addition as the article says (one linked in problem statement).

I.e. you shouldn't allow values grew too large!

shatseli     2020-01-17 23:36:41

Dear Rodion I finally resolved it due to your another hint ( with math.BigInteger class )! I though about the solution you suggested in this thread but it seemed me mathematically wrong to use modulo operation after subsequent add operation ( after multiplicating operation no doubt that it was possuble, fortunately I found the article in magazine "Kwant" of my soviet childhood giving nice examples of using modulo operation :-) - will go to read t now..

Rodion, Anyway thank you for your very interesting challenging task with possibility to learn new Java standard classes and methods and I bag your pardon for wrongly accusing your wonderfull site ! Heva a nice weekind!

Rodion (admin)     2020-01-18 14:16:37
User avatar

Congratulations! I think problems which take more time usually give more profit! :)

I bag your pardon for wrongly accusing

No problem - I understand you spent significant time on debugging and was a bit frustrated :)

I found the article in magazine "Kwant" of my soviet childhood

Wow, my childhood also was in that country - though "Kvant" was a bit tough for me, I preferred "Yuny Technik" :)

It's good you find some sources to help understanding - solution via modulo arithmetic is not critical for this task (as you see, bigint works) - but for some later tasks, like this or about cryptography - bigint won't work taking too much time :)

shatseli     2020-01-19 13:45:19

Hi Rodion

In this thread you provided a reference to the ideone site which looks me as may be very usefull for checking various solution for the same taks.

Please kindly advice :

1. How I can provide in this site the input in the form of text field
   ( as on Abbeycode or your example reference) ?
    I clicked the Stdin button but nothing happened :-( 

2. Does this ideone provide debug possibilities ?

Have a mice week !

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