Reputation: 634
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?
Upvotes: 0
Views: 185
Reputation: 35011
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
Calculating the day:
the calculation for this is just numDays % daysOfTheWeek. If you need to use BigInteger, it has a mod(...)
method
Upvotes: 2