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: in php
Answers
Answered by
3
hey mate here is your answer..
function digPow(n, p) { var m = n; var i, sum = 0; var j = 0; var l = n.toString().length; var digits = []; while (n >= 10) { digits.unshift(n % 10); n = Math.floor(n / 10); } digits.unshift(n); for (i = p; i < l + p; i++) { sum += Math.pow(digits[j], i); j++; } if (sum % m == 0) { return sum / m; } else return -1; } alert(digPow(89, 1))
hope it helps you dear
function digPow(n, p) { var m = n; var i, sum = 0; var j = 0; var l = n.toString().length; var digits = []; while (n >= 10) { digits.unshift(n % 10); n = Math.floor(n / 10); } digits.unshift(n); for (i = p; i < l + p; i++) { sum += Math.pow(digits[j], i); j++; } if (sum % m == 0) { return sum / m; } else return -1; } alert(digPow(89, 1))
hope it helps you dear
pb65Jatt:
nice sis
Similar questions
Chemistry,
7 months ago
Math,
7 months ago
CBSE BOARD XII,
7 months ago
Political Science,
1 year ago
Physics,
1 year ago