Some numbers have funny properties. For example:$$\eqalign{89 & 8^1 + 9^2 &= 89 &= 89  1 \\ 695 & 6^2 + 9^3 + 5^4 &= 1390 &= 695  2 \\ 46288 & 4^3 + 6^4 + 2^5 + 8^6 + 8^7 &= 2360688 &= 46288  51}$$Given a positive integer \$n\$ written in decimal as \$abcd\dotso\$, and a positive integer \$p\$, we want to find a positive integer \$k\$, if it exists, such that the sum of the digits of \$n\$ taken to the successive powers \$p\$, \$p+1\$, \$p+2\$, , is equal to \$nk\$. In other words:Is there an integer \$k\$ such that \$ a^p + b^{p+1} + c^{p+2} + d^{p+3} + \dotsb = nk\$?If it is the case we will return \$k\$, if not return \$-1\$.Note: \$n\$, \$p\$ will always be given as strictly positive integers.dig_pow(89, 1) should return \$1\$ since \$8^1 + 9^2 = 89 = 89  1\$.dig_pow(92, 1) should return \$-1\$ since there is no \$k\$ such that \$9^1 + 2^2\$ equals \$92k\$dig_pow(695, 2) should return \$2\$ since \$6^2 + 9^3 + 5^4 = 1390 = 695  2\$dig_pow(46288, 3) should return \$51\$ since \$4^3 + 6^4 + 2^5 + 8^7 + 8^6 = 2360688 = 46288  51\$import mathdef dig_pow(n,p):    '''     Formula :    (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k    '''    ''' calculate LHS '''    digits, temp = [], n    for i in range(len(str(n))):        digits.append(temp%10)        temp = temp / 10    Tsum = 0    for i in reversed(digits):        Tsum = Tsum + math.pow(i,p)        p = p + 1    ''' Calculate RHS '''    if Tsum % n == 0:        return Tsum / n    else:        return -1print dig_pow(46288, 3)print dig_pow(89, 1)print dig_pow(695, 2)print dig_pow(92, 1)Please feel free to suggest better/faster approaches.
Sum of successive powers of digits
python;algorithm;mathematics