Your range is too large.
The problem asks for natural numbers of the form \$a^b\$. Natural number start at one (1, 2, 3, ...); whole numbers start at zero (0, 1, 2, 3, ...). If the problem asked for the minimum of the sum of the digits, you’d return zero, while the correct answer would be 1.
Perhaps more importantly, you’re flirting with non-deterministic behaviour. Any positive number to the power of zero is one (\$a^0 = 1\$), and zero raised to a positive number is zero (\$0^b = 0\$); \$0^0\$ is mathematically undefined. Python should return NaN
for that expression. And the sum of digits of NaN
is ValueError
.
You should use for num in range(1, n)
to avoid both the technical NaN
and the non-natural number results.
Since \$1^b = 1\$, for efficiency, you could shrink the search space further: for num in range(2, n)
, avoiding the first 100 repeated digit sums of the same natural number.
You could also eliminate another 100 digit sums of 1
by eliminating the 0th powers (for power in range(1, n)
). But technically, you’ve now eliminated the digit sum of the value 1
, so you should make sure it doesn’t affect the answer, or include that afterwards, such as by max(..., default=get_digit_sum(1))
.
You are still brute-forcing the answer. Look for ways to reduce the problem space.
get_digit_sum(99**99) = 936
. The smallest number with a digit sum of 936
would contain 936/9 = 104
nine digits. If \$a^b < 10^{104}\$, it doesn’t contain enough digits, so you don’t need to compute the digit sum for that value, and can avoid converting the number to a string, separating it into digits, converting those to int’s, and summing them together.
Better: compute powers in the reverse order. If \$a^b < 10^{104}\$, you can break out of the inner loop.
Even better: compute the bases in the reverse order also. If \$a^{99} < 10^{104}\$, you can break out of the outer loop too.
But don’t hard code \$10^{104}\$ in your program. It should calculate that limit itself.
Even better: make the threshold dynamic. If the digit sum goes up by over 9, your minimum digit count goes up by 1, and your minimum threshold goes up as well.