Code example
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n.
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k. If it is the case we will return k, if not return -1.
const digPow = (n, p) => {
const numbersArray = n.toString().split('')
let pow = p
let sum = 0
for (let i = 0; i < numbersArray.length; i++) {
sum += Math.pow(+numbersArray[i], pow++)
}
if (sum % n !== 0) {
return -1
}
return sum / n
}
Courses
Last completed and current courses: