0

I'm working on a program that reads from a file some numbers. With those numbers, then the program needs to calculate a result number using a formula I will provide later.

File format looks like:

3 //number of tests
28347823734 /*argument 1 of test 1*/ 78353845787 /*argument 2 of test 1*/
63747289347 /*argument 1 of test 2*/ 242489983758 /*argument 2 of test 2*/
75472934872 /*argument 1 of test 3*/ 627364829374 /*argument 2 of test 3*/

After parsing each value from each test, the formula that has to be calculated to obtain a result is the following:

result = argument1 * 8 * argument2

Later on the program, I have to relate the result with the day of the week. For example, if the result was 2, the day would be Wednesday and if the result was 8, the day would be Tuesday. To handle this, I execute the following method. (result needs to be divided by 8)

String str = calculateDay(result/8);

Then to know the day of the week I created the following method:

private String calculateDay (long n) {

        String days = new String[]{"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"};
        int d = 0;

        for (long i=1; i<=n; i++) {
            if (d==6) {
                d=0;
                continue;
            }

            d++;
        }

        return days[d];
    }

The issue here is that, for a text file with 100 different tests, this will take a long while to fully execute as the numbers are really big so... how could I handle this?

user157629
  • 624
  • 4
  • 17
  • 1
    This reads like it's from some coding competition website. The way you are calculating day of the week is linear time when you should be able do it in constant time using modulo arithmetic. Hint: what does `d = n%7` return? How does that compare to the result of the `for` loop in your `calculateDay` function? – pault Apr 20 '20 at 17:42
  • Doesn't your `for` loop in `calculateDay` just do modulo operation? Why not `return days[n % 7]`? (note your check for `d==6` seems incorrect since you have 7 days, not 6). – lurker Apr 20 '20 at 17:45
  • @lurker re modulo operation: Yes, and it does it quite inefficiently – ControlAltDel Apr 20 '20 at 17:47
  • Does this answer your question? [How to handle very large numbers in Java without using java.math.BigInteger](https://stackoverflow.com/questions/5318068/how-to-handle-very-large-numbers-in-java-without-using-java-math-biginteger) – izhang05 Apr 20 '20 at 17:47
  • 1
    @ControlAltDel obviously, my point. – lurker Apr 20 '20 at 17:49
  • Oh you guys are right. Modulo operation would work, I don't know how I couldn't think about it before. Also @lurker `d==6` worked fine in an easier problem, because the array `days` has 7 positions that goes from 0 (MONDAY) to 6 (SUNDAY). Also yes this is for a competition website as I am trying to improve on Java! – user157629 Apr 20 '20 at 17:53
  • @user157629 if you have `d==6` in your current logic, your results will always range 0-5 since a value of 6 will force a reset to 0. – lurker Apr 20 '20 at 17:58

1 Answers1

2
  1. Big numbers: These numbers look like they should fit quite comfortably within a long value. But if long is not big enough, you can use java.Math.BigInteger

  2. Calculating the day: the calculation for this is just numDays % daysOfTheWeek. If you need to use BigInteger, it has a mod(...) method

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80