JavaScript 中的普洛尼克數


普洛尼克數是由兩個連續整數相乘得到的,即 n(n + 1) 形式的數字。

我們需要編寫一個 JavaScript 函式,它接受一個數字並返回 true(如果是普洛尼克數)或返回 false(如果不是)

讓我們編寫此函式的程式碼 -

示例

const num = 90;
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

更新於: 14-Sep-2020

165 次瀏覽

啟動您的 職業

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.