JavaScript 中的特殊数字类型 (pronic)

javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/25 4:28:17

我们需要编写一个 JavaScript 函数,该函数接受一个数字,如果该数字是 Pronic 数字,则返回 true,否则返回 false。

Pronic 数字是两个连续整数的乘积,即形式为 −

n(n + 1)

示例

其代码为 −

const num = 132;
const isPronic = num => {
   let nearestSqrt = Math.floor(Math.sqrt(num)) - 1;
   while(nearestSqrt * (nearestSqrt + 1) <= num){
      if(nearestSqrt * (nearestSqrt+1) === num ){
         return true;
      };
      nearestSqrt++;
   };
   return false;
};
console.log(isPronic(num));

输出

控制台中的输出 −

true

相关文章


有用资源