Solution to Project Euler Problem 56: Powerful digit sum - A googol (10^100) is a massive number: one followed by one-hundred zeros; 100^100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
Considering natural numbers of the form, a^b, where a, b < 100, what is the maximum digital sum?
Updated: May 29, 2023 — Training Time: 2 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 56: Powerful digit sum.
Difficulty: Easy.
Objective: A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
Considering natural numbers of the form, ab, where a, b < 100, what is the maximum digital sum?
Input: None.
Expected Output: 972.
Sikademy Solution in Java Programming Language
package sikademy;
/**
*
* @author Archangel Macsika
* Copyright (c) Sikademy. All rights reserved
*/
import java.math.BigInteger;
public class SikademyEulerSolution {
public String run() {
int max = 0;
for (int a = 1; a < 100; a++) {
for (int b = 1; b < 100; b++) {
BigInteger pow = BigInteger.valueOf(a).pow(b);
max = Math.max(digitSum(pow), max);
}
}
return Integer.toString(max);
}
private static int digitSum(BigInteger n) {
int sum = 0;
String s = n.toString();
for (int i = 0; i < s.length(); i++)
sum += s.charAt(i) - '0';
return sum;
}
public static void main(String[] args) {
SikademyEulerSolution solution = new SikademyEulerSolution();
System.out.println(solution.run());
}
}
Sikademy Solution in Python Programming Language
#
# @author Archangel Macsika
# Copyright (c) Sikademy. All rights reserved.
#
def compute():
ans = max(sum(int(c) for c in str(a**b))
for a in range(100) for b in range(100))
return str(ans)
if __name__ == "__main__":
print(compute())