Skip to content

Commit

Permalink
refactor: Challenges class as PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ferodrigop committed Nov 22, 2024
1 parent 32577dd commit e235614
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/main/java/Challenges.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public String[] circularArray(int index) {
System.arraycopy(COUNTRY_NAMES, index, tempArray, 0, numberOfElementsToCopy);
System.arraycopy(COUNTRY_NAMES, 0, tempArray, numberOfElementsToCopy, COUNTRY_NAMES.length - numberOfElementsToCopy);

COUNTRY_NAMES = tempArray.clone();
return COUNTRY_NAMES;
return tempArray.clone();
}

;
Expand All @@ -91,17 +90,18 @@ public String[] circularArray(int index) {
***** */

public String ownPower(int number, int lastDigits) {
BigInteger powResult = BigInteger.ZERO;
BigInteger moduloNumber = BigInteger.TEN.pow(lastDigits);
BigInteger moduloAddition = BigInteger.ZERO;

for (int i = 1; i <= number; i++) {
BigInteger powNumber = new BigInteger(String.valueOf(i)).pow(i);
BigInteger resultModuloPow = BigInteger.valueOf(i).modPow(BigInteger.valueOf(i), moduloNumber);

powResult = powResult.add(powNumber);
moduloAddition = moduloAddition.add(resultModuloPow).mod(moduloNumber);
}

String result = String.valueOf(powResult);
String result = String.valueOf(moduloAddition);

return result.substring(result.length() - lastDigits);
return "0".repeat(lastDigits - result.length()).concat(result);
}

;
Expand All @@ -125,17 +125,15 @@ A factorial (x!) means x! * (x - 1)... * 3 * 2 * 1.

public Integer digitSum(int n) {
BigInteger totalFactorial = BigInteger.ONE;
BigInteger sum = BigInteger.ZERO;

for (int i = n; i >= 1; i--) {
totalFactorial = totalFactorial.multiply(BigInteger.valueOf(i));
}

char[] digits = String.valueOf(totalFactorial).toCharArray();

BigDecimal sum = BigDecimal.ZERO;

for (char c : digits) {
sum = sum.add(new BigDecimal(String.valueOf(c)));
while (totalFactorial.compareTo(BigInteger.ZERO) > 0) {
sum = sum.add(totalFactorial.mod(BigInteger.TEN));
totalFactorial = totalFactorial.divide(BigInteger.TEN);
}

return sum.intValue();
Expand Down

0 comments on commit e235614

Please sign in to comment.