Near Power Sums

A positive integer, $n$, is a near power sum if there exists a positive integer, $k$, such that the sum of the $k$th powers of the digits in its decimal representation is equal to either $n+1$ or $n-1$. For example $35$ is a near power sum number because $3^2+5^2 = 34$.

Define $S(d)$ to be the sum of all near power sum numbers of $d$ digits or less.
Then $S(2) = 110$ and $S(6) = 2562701$.

Find $S(16)$.

Solving for $S(16)$ with a brute force approach would require checking and testing a virtually endless list of 16-digit numbers, which is not feasible. Therefore, we will need to approach this problem systematically and programmatically.

For each digit, $k$, and each possible sum, $n$, we want to know how many ordered lists of $d$ digits will produce a sum of $n$ where each digit is raised to the $k$-th power. Given this, we can create a “table” with the numbers 0 to 9 (for the digits), all possible values for sums (from 0 to the sum of the 16th power of 9), and for each power $k$ (from 1 to 16).

We need to initialize the code such that there is 1 way of getting a sum of 0 from 0 digits (using no digits at all) for every power. Hence, $T[0][0][i] = 1$ for all $i$ from 1 to 16.

We will use a recursive routine to find the number of ways for each sum for each count of digits using values 0-9 for each power. Then we iterate over powers, digits, and numbers. For each number, we add on to the count which is determined by subtracting the kth power of that number from the sum and looking at the remaining number of digits minus 1.

We can then look at the results and consider the possible sums. We start at 1 to avoid the edge case of 0 that doesn’t fit the problem’s pattern. We look at sums which are off by 1 from a multiple of a power and if the sum works, we add its count times the power to our answer.

We exclude numbers above $10^{16}$, which we consider as overcounts. Also, we need to handle overcounts due to leading zeroes in numbers less than $10^{16}$.

Using a high-level programming language such as Python (with its easy-to-use numerical operations and data structures), combined with the concept of dynamic programming, is a perfect tool for this problem.

The mathematical insight and the programming knowledge required to efficiently solve this problem is quite high, and a full-blown implementation would be out of scope for this explanation. However, a detailed set of pseudocode is provided to show the general flow of the approach:

“`python
# Initialize the 4D list with zeros
T = [[[0 for _ in range(MAX_POWER*9*DIGITS+1)]
for _ in range(DIGITS+1)] for _ in range(10)]

# Fill the base cases for each power
for i in range(MAX_POWER+1):
T[0][0][i] = 1

# Populate the rest of the 4D list
for k in range(1, MAX_POWER+1):
for d in range(1, DIGITS+1):
for n in range(10):
for sum in range(n**k, MAX_POWER*9*DIGITS+1):
T[n][d][sum] += T[n-1][d-1][sum – n**k]

# This will hold the result
S = 0

# For each k
for k in range(1, MAX_POWER+1):
# For each sum
for sum in range(MAX_POWER*9*DIGITS+1):
# If the sum is 1 mod k
if sum % k == 1:
# We have a hit, add to the result
S += T[9][DIGITS][sum]*k

# return S
return S
“`

This high-level pseudocode outlines how dynamic programming can be used to solve this problem. As last note, due to the complexity of the problem, adjustment, optimization and several debuggings need to be done in the actual implementation.

More Answers:
Sum of Squares II
A Messy Dinner
Triangular Pizza

Error 403 The request cannot be completed because you have exceeded your quota. : quotaExceeded

Share:

Recent Posts

Mathematics in Cancer Treatment

How Mathematics is Transforming Cancer Treatment Mathematics plays an increasingly vital role in the fight against cancer mesothelioma. From optimizing drug delivery systems to personalizing

Read More »