检查 Disarium 数 - JavaScript

javascriptweb developmentfront end technologyobject oriented programming更新于 2024/4/23 14:13:00

Disarium 数 − 所有满足以下等式的数都是 dDisarium 数 −

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

相关文章