In Problem 14 the intermidiate results are going above long range and producing abnormal results

Back to Problem Solutions forum

Ujjwal Gupta     2021-04-06 19:54:25

This is the code:

public class ModularCalculator
{
    public static void main(String[] args)
    {
        int initValue = 5180;
        int[] numbers = new int[] {2, 432, 17, 220, 2, 828, 1, 537, 5768, 3, 4464, 349, 9, 70, 1, 708, 414, 5, 6518, 473, 528, 56, 191, 5, 8243, 2316, 57, 3, 85, 5, 7, 70, 7419, 10, 5926, 5, 286, 323, 6793, 24, 32, 297, 470, 7034, 4871, 4, 5, 81, 127, 7};
        char[] operand = new char[] {'*', '*', '*', '+', '*', '+', '+', '*', '+', '*', '*', '*', '+', '*', '*', '*', '+', '*', '*', '+', '+', '+', '*', '*', '*', '*', '+', '+', '*', '+', '+', '*', '+', '*', '*', '*', '+', '+', '*', '+', '+', '*', '+', '+', '*', '*', '*', '+', '*', '*'};
        int finalValue = 8302;

        long resultant = 0;

        resultant += initValue;

        for(int i = 0; i < numbers.length; i++)
        {
            if(operand[i] == '+')
            {
                resultant += numbers[i];
            }
            else if(operand[i] == '*')
            {
                resultant *= numbers[i];
            }

            System.out.println(resultant);
        }

        resultant %= finalValue;

        System.out.println("Answer: " + resultant);
    }
}

The output for this code is:

10360
4475520
76083840
76084060
152168120
152168948
152168949
81714725613
81714731381
245144194143
1094323682654352
381918965246368848
381918965246368857
8287583493536268374
8287583493536268374
1544497984040594904
1544497984040595318
7722489920202976590
-5975277270364946444
-5975277270364945971
-5975277270364945443
-5975277270364945387
2420173930287631275
-6345874422271395241
5923330257177411413
-5944715217021569796
-5944715217021569739
-5944715217021569736
-7238703456675533928
-7238703456675533923
-7238703456675533916
-8647151977129480488
-8647151977129473069
5762200597253027390
1877458885060271924
-9059449648408191996
-9059449648408191710
-9059449648408191387
-2503231741779900915
-2503231741779900891
-2503231741779900859
-5590064360248490483
-5590064360248490013
-5590064360248482979
-1809245975062405493
-7236983900249621972
708568646170993372
708568646170993453
-2245502304831589549
2728227939888424773
Answer: 3649

I have been stuck on this for a while.

gardengnome     2021-04-06 20:05:10
User avatar

The very first paragraph of this problem states: "This task provides practice for core property of remainder taking operation in arithmetic - persisting of the remainder over addition and multiplication. This important property is often used for checking results of calculations, in competitive programming, in calculation checksums and especially for encryption. See Modular arithmetic for thorough explanations."

Follow the link, read carefully, and then think how you can modify your program to avoid going beyond the long range.

Ujjwal Gupta     2021-04-06 20:18:39

Thank you so much

I did this and it worked:

public class ModularCalculator
{
    public static void main(String[] args)
    {
        int initValue = 5180;
        int[] numbers = new int[] {2, 432, 17, 220, 2, 828, 1, 537, 5768, 3, 4464, 349, 9, 70, 1, 708, 414, 5, 6518, 473, 528, 56, 191, 5, 8243, 2316, 57, 3, 85, 5, 7, 70, 7419, 10, 5926, 5, 286, 323, 6793, 24, 32, 297, 470, 7034, 4871, 4, 5, 81, 127, 7};
        char[] operand = new char[] {'*', '*', '*', '+', '*', '+', '+', '*', '+', '*', '*', '*', '+', '*', '*', '*', '+', '*', '*', '+', '+', '+', '*', '*', '*', '*', '+', '+', '*', '+', '+', '*', '+', '*', '*', '*', '+', '+', '*', '+', '+', '*', '+', '+', '*', '*', '*', '+', '*', '*'};
        int finalValue = 8302;

        long resultant = 0;

        resultant += initValue;

        for(int i = 0; i < numbers.length; i++)
        {
            if(operand[i] == '+')
            {
                resultant = (resultant + numbers[i]) % finalValue;
            }
            else if(operand[i] == '*')
            {
                resultant = (resultant * numbers[i]) % finalValue;
            }
        }

        resultant %= finalValue;

        System.out.println(resultant);
    }
}
Rodion (admin)     2021-04-06 20:21:52
User avatar

new int[] {2, 432, 17, 220

Also note that you'd better read input with Scanner or something like this (you can copy-paste the whole input to your local console - or run the program on the site).

Manually rewriting input data as array isn't convenient (as you probably already found).

See gardengnome's solution for example!

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