Playing with digits - mrm7moud/codewars GitHub Wiki

Some numbers have funny properties. For example:

89 --> 8¹ + 9² = 89 * 1

695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2

46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51 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. In other words:

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.

Note: n, p will always be given as strictly positive integers.

digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1 digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2 digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51

###Solution

function digPow(n, p){ // ... var digits = n.toString().split(''); var result = 0; for(var i=0; i<digits.length; i++){ result = result + Math.pow(digits[i], p); p++; } var data = result/n; if(result % n === 0){ return data; }else{ return -1; } }

or

function digPow(n, p) { var x = String(n).split("").reduce((s, d, i) => s + Math.pow(d, p + i), 0) return x % n ? -1 : x / n }

or

function digPow(n, p){ var ans = (''+n).split('') .map(function(d,i){return Math.pow(+d,i+p) }) .reduce(function(s,v){return s+v}) / n return ans%1 ? -1 : ans
}//z.

or

function digPow(n, p){ var ans = n.toString().split('') .map((v,i) => Math.pow(parseInt(v), i+p)) .reduce((a,b) => a+b) / n; return ans%1 == 0 ? ans : -1; }

or

function digPow(n, p){ // ... var num1=n.toString(); var num2=p.toString(); var sum=0;

for(var i=0;i<num1.length;i++){ sum=sum+Math.pow(parseInt(num1.charAt(i)),p); p++; if(sum%n==0){return sum/n;} } return -1;

}

or

function digPow(n, p){ var str = n.toString() var res = 0;

for(var i=0; i<str.length; i++){ res += Math.pow(parseInt(str[i]),p++); }

return res%n === 0 ? res/n : -1; }

or

function digPow(n, p){ let total = Reflect.apply(Array.prototype.reduce, Reflect.apply(Array.prototype.map, n.toString(), [(i) => parseInt(i)]), [(prev, curr, i) => prev + Math.pow(curr, i + p), 0]);

let division = total / n;

if(total % n > 0) { return -1; }

return division; }

or

function digPow(n, p){ if(n <= 0 || !p) return -1; var total = 0, numstr = ""+n, tmp;

for(var i=0; i<numstr.length; i++) { total += Math.pow(numstr[i], p++); }

return (total/n)%1===0 ? total/n : -1; }

or

function digPow(n, p) { var summary = (n + '').split('').reduce(function(accumulator, currentValue) { return accumulator + Math.pow(currentValue, p++); }, 0);

return summary === n ? 1 : !(summary % n) ? summary / n : -1; }

or

function digPow(n, p){ var product = n.toString().split('').map((v,i) => Math.pow(Number(v), p+i)).reduce((s,v) => s+v, 0); return (product % n == 0) ? (product / n) : -1; }