Home > Web Front-end > JS Tutorial > Calculate the value of (m)1/n in JavaScript

Calculate the value of (m)1/n in JavaScript

WBOY
Release: 2023-08-25 09:25:10
forward
944 people have browsed it

在 JavaScript 中计算 (m)1/n 的值

In the field of JavaScript programming, the ability to calculate (m) raised to the power 1/n is very important because it allows developers to perform complex mathematical operations accurately and efficiently. This article takes advantage of the computational power of JavaScript to delve into the complexities of calculating such exponential values. By exploring the underlying algorithms and employing rarely used mathematical functions, we'll provide developers with the knowledge and tools they need to seamlessly perform these calculations in their JavaScript programs. Join us on this inspiring journey as we uncover the secrets of 1/n power calculations (m), empowering developers to tackle mathematical challenges with new confidence.

Math.pow() function

The Math.pow() function is a built-in function in the JavaScript Math object that allows you to calculate a base multiplied by an exponent raised to a power. It takes two parameters: base and exponent.

The syntax for using Math.pow() is as follows -

Math.pow(base, exponent);
Copy after login

Here, the base represents the power of the number you want, and the exponent represents the power of the base you want.

Problem Statement

Given two positive integers, a base integer m and an exponential integer n, determine the value of the nth root of m, expressed as m^(1/n). Returns the result rounded to the nearest integer.

Example input -

m = 64, n = 3
Copy after login

Example output -

4
Copy after login

method

In this article we will see a number of different ways to solve the above problems in JavaScript -

  • Math.pow and Math.exp

  • Newton’s method

  • Binary search

Method 1: Math.pow and Math.exp

This method uses the Math.pow() function to calculate the nth root of a number. It involves one line of code: root = Math.pow(m, 1/n). By raising m to the power 1/n, it makes it straightforward to compute the required roots. This method is convenient, direct, and provides a quick solution without the need for custom root-finding algorithms.

Example

In this code snippet, the Math.pow() function is used to calculate the nth root of a given number. Use the formula Math.pow(m, 1/n), where m is the number to find the root and n is the order of the root. The resulting value is stored in the root variable and subsequently displayed on the console.

let m = 27;
let n = 3;
let root = Math.pow(m, 1/n);
console.log(root);
Copy after login

Output

The following is the console output -

3
Copy after login

Method 2: Newton’s method

Newton's method is an iterative algorithm used to approximate the roots of a function. When finding the nth root of a number m, we start with an initial guess of m/n, using Newton's method. The algorithm then iteratively refines the guess using the formula x = ((n - 1) * x m / Math.pow(x, n - 1)) / n . Iteration continues until the difference between Math.pow(x, n) and m is less than the specified tolerance. The resulting x value represents the approximate nth root of m.

Example

​​nthRoot function computes the nth root of a given number (m) with optional precision (tolerance). The initial guess for the root is set to m divided by n. Iteratively refine the guess through a while loop until the difference between Math.pow(x, n) and m becomes less than the tolerance. Newton's method formula is used in each iteration to get a better approximation: x = ((n - 1) * x m / Math.pow(x, n - 1)) / n. Finally returns the final approximation of the root.

function nthRoot(m, n, tolerance = 0.0001) {
   let x = m / n; // Initial guess

   while (Math.abs(Math.pow(x, n) - m) > tolerance) {
      x = ((n - 1) * x + m / Math.pow(x, n - 1)) / n;
   }
   return x;
}
let m = 27;
let n = 3;
let root = nthRoot(m, n);
console.log(root);
Copy after login

Output

The following is the console output -

3.000000068671529
Copy after login

Method 3: Binary search

The binary search method is used to find the nth root of the number m. It initializes the search range with low = 0 and high = max(1, m). By calculating the midpoint as mid, mid raised to the nth power is determined as the guess value. Depending on whether the guessed value is greater or less than m, the low or high value is updated, thus halving the search range. Iteration continues until the difference between the high and low points is less than the specified tolerance. The final value of mid is approximately the nth root of m.

Example

nthRoot function takes m, n, and optional tolerance as parameters. The low and high variables are initialized to 0 and max(1, m) respectively. The while loop continues until the difference between the high and low is greater than the tolerance. In each iteration, the midpoint (mid) is calculated. The guess variable stores mid raised to the nth power. Depending on whether the guess is greater or less than m, update the low or high value to narrow the search. When the loop ends, the final mid value is returned as the approximate nth root of m.

function nthRoot(m, n, tolerance = 0.0001) {
   let low = 0;
   let high = Math.max(1, m);
   let mid;

   while (high - low > tolerance) {
      mid = (low + high) / 2;
      let guess = Math.pow(mid, n);

      if (guess < m) {
         low = mid;
      } else if (guess > m) {
         high = mid;
      } else {
         break;
      }
   }
   return mid;
}
let m = 27;
let n = 3;
let root = nthRoot(m, n);
console.log(root);
Copy after login

Output

The following is the console output -

3.000040054321289
Copy after login

in conclusion

Ultimately, the process of computing the value of (m) raised to the power 1/n in JavaScript presents an interesting computational challenge that can be solved elegantly by implementing an appropriate algorithm. Although less common, this kind of mathematical operation is of great significance in various fields such as cryptography, scientific modeling and data analysis. By leveraging the power of JavaScript and employing precise methods, programmers can efficiently evaluate this expression, unlocking new possibilities and enabling the development of complex applications. In summary, mastering the calculation of (m)1/n in JavaScript expands the mathematical capabilities available to programmers, fosters innovation, and enables the implementation of complex mathematical concepts in the world of web development.

The above is the detailed content of Calculate the value of (m)1/n in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template