# Optimizing the Dig Pow function

I have written a Python function to solve the Dig Pow problem, where the goal is to find a number k such that the sum of each digit of n raised to a specific and increasing power equals n * k.

Here is my current code:

def dig_pow(n, p):
if n != int(n):
return -1
stockn = n
tab = []
while n > 0:
newn = n % 10
tab.append(newn)
n //= 10
tab = tab[::-1]
tabRes = []
for i in range(len(tab)):
tabRes.append(pow(tab[i], i + p))
sum_pow = sum(tabRes)
k = sum_pow // stockn
if sum_pow % stockn == 0:
return k
else:
return -1



I would like to get some suggestions on how to improve code readability, and efficiency. Any other recommendations to optimize the code and make it run faster would also be appreciated.

• Do you have any unit tests for this? I'm not sure from your description what results are expected. May 10, 2023 at 7:22

Do not merge this code down to main.

    tabRes = []


Pep-8 asks that you spell it tab_res. I ask that you explain what the variable is intended to represent, perhaps as a # comment.

If there is some reference you are following for this implementation, you should cite it.

It is unclear what dig_pow(n, p) is supposed to return.

the sum of each digit of n raised to a specific and increasing power equals n * k.

That English sentence could be a spec, maybe. The code seems to implement something else.

Absent unit tests or an intelligible specification, it is unclear whether the OP code accomplishes its design goals.

I would not be willing to delegate or accept maintenance tasks on this codebase.