Project Euler Problem 56 Statement
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 \lt 100$, what is the maximum digital sum?
Python Source Code
N = int(input())
r = range(3*N // 4, N)
print(max(sum(map(int, str(a**b))) for a in r for b in r))
HackerRank version
HackerRank Project Euler 56: Considering natural numbers of the form, $a^b$ , where $a,b\lt N$, and $(5\le N\le 200)$ what is the maximum digital sum?
Solution
Optimization with Range r: By limiting \( a \) and \( b \) to the upper quarter of the range (\( 75-99 \) for \( N = 100 \)), the code reduces the number of computations. This is based on the observation that larger exponents and bases are more likely to yield higher digit sums, making the search more efficient without missing the optimal solution.
Digit Sum Calculation: Converting \( a^b \) to a string and then mapping each character back to an integer allows for an easy way to calculate the sum of its digits.
Max Function: Finally, the max
function retrieves the highest digital sum from all possible combinations within the optimized range.