Disarium Number-满足以下等式的所有数字均为dDisarium number-
xy...z = x^1 + y^2 + ... + z^n
其中n是数字中的位数。
例如-
175 is a disarium number be: 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175
让我们为该函数编写代码-
以下是代码-
const num = 175; const isDisarium = num => { const res = String(num) .split("") .reduce((acc, val, ind) => { acc += Math.pow(+val, ind+1); return acc; }, 0); return res === num; }; console.log(isDisarium(num)); console.log(isDisarium(32)); console.log(isDisarium(4334));
输出结果
控制台中的输出:-
true false false