如何在 JavaScript 中計算一個數的 n 次方根?


我們給定一個數字,任務是使用 JavaScript 計算該數字的 n 次方根。我們可以透過多種方法實現這一點,其中一些如下所示。

  • 使用 Math.pow() 方法

  • 使用對數

要獲得一個數的 n 次方根,首先讓我們瞭解一下我們可以計算哪些數的根,以及哪些數的根我們無法計算。

  • 如果數字為正且根為偶數,則我們將得到兩個解。**例如** - 16 的 2 次方根是 +4 和 -4,類似地,16 的 4 次方根是 +2 和 -2

  • 如果數字為負且根為奇數,則我們將得到一個負解。**例如** - -27 的 3 次方根僅為 -3(而非 3)。

  • 如果數字為正且根為奇數,則我們將得到一個正解。**例如** - 27 的 3 次方根為 3

  • 如果數字為負且根為偶數,則沒有解。

使用 Math.pow() 方法

JavaScript 中的 Math.pow() 函式用於獲取一個數的冪。即某個指數的數字值。此方法接受兩個引數,一個是底數,另一個是指數。我們使用什麼樣的引數並不重要,例如整數、分數、負數等,它可以取任何值並進行運算。

語法

Math.pow( X, Y )

引數

  • X - 這是我們想要計算冪的底數。

  • Y - 這是指數

步驟

您可以按照以下步驟使用 Math.power() 方法查詢一個數的 n 次冪

步驟 1 - 檢查底數是正數還是負數。以及 n 次方根是奇數還是偶數。

步驟 2 - 如果底數為正且根為偶數。答案將是結果的正數和負數版本。

步驟 3 - 如果底數為正且根為奇數。答案將是結果的正數版本。

步驟 4 - 如果底數為負且根為偶數,則答案將是結果的負數版本。

步驟 5 - 如果底數為負且根為奇數,則沒有答案。

注意 - 在 Math.pow() 中,我們將始終傳遞底數的絕對值。

示例

在此示例中,我們計算各種數字的 n 次方根。

<html lang="en">
   <body>
      <h2> Calculating the nth root of a number in JavaScript </h2>
      <p>Here are the nth root of the following values</p>
      <p id="p1"></p> <p id="p2"></p> <p id="p3"></p>
      <script>
         calcRoot(16, 2, "p1");
         calcRoot(27, 3, "p2");
         calcRoot(-27, 3, "p3");
         function calcRoot(base, root, id){
         let isBasePositive = base > 0 ? true : false;
         let isRootEven = root % 2 == 0 ? true : false;
         let p = document.getElementById(id);
         if(base == 0){
            p.innerText = `${root}th root of ${base} is 0`
            return
         }
         if(root == 0){
            p.innerText = `${root}th root of ${base} is Infinity`
            return
         }
         if(isBasePositive && isRootEven){
            let ans = Math.pow(base, 1 / root);
            p.innerText = `${root}th root of ${base} is ${ans} and -${ans}`
         }
         if(isBasePositive && !isRootEven){
            let ans = Math.pow(base, 1 / root);
            p.innerText = `${root}th root of ${base} is ${ans}`
         }
         if(!isBasePositive && !isRootEven){
            let ans = Math.pow( Math.abs( base ), 1 / root);
            p.innerText = `${root}th root of ${base} is -${ans}`
         }
         if(!isBasePositive && isRootEven){
               p.innerText = `${root}th root of ${base} is NaN`
            }
         }
      </script>
   </body>
</html>

使用對數

示例

在此方法中,我們使用對數來計算 n 次方根。現在,將此語句轉換為 JavaScript 表示式 -

R = Math.exp(1 / root * Math.log(base))

在此示例中,我們正在計算多個數字的根

<html>
   <body>
      <h2> Calculating the nth root of a number in JavaScript </h2>
      <p>Here are the nth root of the following values</p>
      <p id="p1"></p>
      <p id="p2"></p>
      <script>
         calcRoot(16, 2, "p1")
         calcRoot(49, 2, "p2")
         function calcRoot(base, root, id){
            let r = Math.exp(1 / root * Math.log(base))
            let p = document.getElementById(id);
            p.innerText = `${root}th root of ${base} is ${r}`
         }
      </script>
   </body>
</html>

總之,在 JavaScript 中有多種方法可以計算一個數的 n 次方根。一種方法是使用 Math.pow() 函式,它接受兩個引數:底數和指數。底數是要計算其根的數字,指數是要計算的根。要使用 Math.pow() 獲取一個數的 n 次方根,可以使用以下公式:Math.pow(base, 1/root)。

您還可以使用對數方法,該方法涉及使用 Math.log() 函式計算根。為此,可以使用以下公式:Math.exp(Math.log(base)/root)。重要的是要考慮底數是正數還是負數以及根是偶數還是奇數,因為這會影響可能的解。

更新於: 2023年1月6日

1K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

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