Problem Description Given that today is Saturday, determine which day of the week it will be after 20^22 days. Note that days are represented as 1 (Monday) through 7 (Sunday).
Approach This problem can be efficiently solved using modular arithmetic to avoid computing the extremely large value of 20^22 directly. Since the week cycles every 7 days, we only need to find the remainder when 20^22 is divided by 7. We then adjust this remainder based on the starting day (Saturday) to find the target day.
- Use Java's BigInteger class to compute 20 raised to the 22nd power, as this value exceeds the range of primitive integer types.
- Calculate the remainder of this result when divided by 7 using the mod operation.
- Adjust the remainder by adding the offset from Saturday (represented as 6 in the 1-7 numbering system) and wrap around if necesary to fit the required 1-7 day format.
Code Implementation
import java.math.BigInteger;
public class DayOfWeekCalculator {
public static void main(String[] args) {
BigInteger base = new BigInteger("20");
BigInteger powerResult = base.pow(22);
BigInteger divisor = new BigInteger("7");
BigInteger remainder = powerResult.mod(divisor);
int remValue = remainder.intValue();
int targetDay = (remValue + 6) <=7 ? (remValue +6) : (remValue +6 -7);
System.out.println(targetDay);
}
}
Common BigInteger Methods
add(BigInteger val): Returns a BigInteger representing the sum of this instance and the specified value.subtract(BigInteger val): Returns a BigInteger representing the difference between this instance and the specified value.multiply(BigInteger val): Returns a BigInteger representing the product of this instance and the specified value.divide(BigInteger val): Returns a BigInteger representing the quotient of this instance divided by the specified value.mod(BigInteger val): Returns a BigInteger representing the remainder when this instance is divided by the specified value.pow(int exponent): Returns a BigInteger representing this instance raised to the power of the specified exponent.gcd(BigInteger val): Returns the greatest common divisor of this instance and the specified value as a BigInteger.abs(): Returns a BigInteger representing the absolute value of this instance.negate(): Returns a BigInteger representing the negative of this instance.intValue(): Converts this BigInteger to an int value.longValue(): Converts this BigInteger to a long value.floatValue(): Converts this BigInteger to a float value.doubleValue(): Converts this BigInteger to a double value.
Common BigDecimal Methods
add(BigDecimal augend): Returns a BigDecimal representing the sum of this instance and the specified augend.subtract(BigDecimal subtrahend): Returns a BigDecimal representing the difference between this instance and the specified subtrahend.multiply(BigDecimal multiplicand): Returns a BigDecimal representing the product of this instance and the specified multiplicand.divide(BigDecimal divisor): Returns a BigDecimal representing the quotient of this instance divided by the specified divisor. Throws an ArithmeticException if the division results in a non-terminating decimal.setScale(int newScale, RoundingMode roundingMode): Returns a BigDecimal with the specified number of decimal places, rounded according to the given rounding mode.
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalExample {
public static void main(String[] args) {
BigDecimal exampleDecimal = new BigDecimal("520.13141617");
// Truncate to 7 decimal places
System.out.println(exampleDecimal.setScale(7, RoundingMode.DOWN).doubleValue());
}
}
Output: 520.1314161
compareTo(BigDecimal val): Compares this BigDecimal numerically with the specified value, returning a negative integer, zero, or positive integer as this instance is less than, equal to, or greater than the specified value.equals(Object x): Returns true if and only if the specified Object is a BigDecimal with the same numeric value and scale as this instance.intValue(): Converts this BigDecimal to an int value.longValue(): Converts this BigDecimal to a long value.doubleValue(): Converts this BigDecimal to a double value.floatValue(): Converts this BigDecimal to a float value.