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


In the realm of JavaScript programming, the ability to calculate the value of (m) to the power 1/n holds significant importance, as it enables developers to perform complex mathematical operations with precision and efficiency. Harnessing the power of JavaScript's computational capabilities, this article dives into the intricacies of calculating such exponential values. By exploring the underlying algorithms and employing rarely used mathematical functions, we will equip developers with the knowledge and tools necessary to seamlessly perform these calculations in their JavaScript programs. Join us on this enlightening journey as we unravel the secrets of computing (m) to the power 1/n, empowering developers to tackle mathematical challenges with newfound confidence.

Math.pow() Function

The Math.pow() function is a built-in function in JavaScript's Math object that allows you to calculate the power of a base number raised to an exponent. It takes two arguments: the base number and the exponent.

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

Math.pow(base, exponent);

Here, base represents the number you want to raise to a power, and exponent represents the power to which you want to raise the base number.

Problem Statement

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

Sample Input −

m = 64, n = 3

Sample Output −

4

Approach

In this article, we are going to see a number of different ways to solve the above problem statement in JavaScript −

  • Math.pow and Math.exp

  • Newton’s Method

  • Binary Search

Method 1: Math.pow and Math.exp

This approach uses the Math.pow() function to calculate the nth root of a number. It involves a single line of code: root = Math.pow(m, 1/n). By raising m to the power of 1/n, it directly computes the desired root. This method is convenient and straightforward, providing a quick solution without the need for a custom root-finding algorithm.

Example

In this code snippet, the Math.pow() function is utilized to calculate the nth root of a given number. The formula Math.pow(m, 1/n) is used, where m represents the number for which the root is sought, and n denotes the root's order. 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);

Output

The following is the console output −

3

Method 2: Newton’s Method

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

Example

​​The nthRoot function calculates the nth root of a given number (m) with an optional precision (tolerance). The initial guess for the root is set as m divided by n. Through a while loop, the guess is iteratively refined until the difference between Math.pow(x, n) and m becomes smaller than the tolerance. Newton's method formula is employed in each iteration to obtain a better approximation: x = ((n - 1) * x + m / Math.pow(x, n - 1)) / n. Ultimately, the final approximation of the root is returned.

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);

Output

The following is the console output −

3.000000068671529

Method 3: Binary Search

The binary search approach is used to find the nth root of a number m. It initializes a search range with low = 0 and high = max(1, m). By calculating the midpoint as mid, it determines the value of guess by raising mid to the power of n. Depending on whether guess is greater or smaller than m, the low or high value is updated, halving the search range. The iteration continues until the difference between high and low is smaller than the specified tolerance. The final value of mid approximates the nth root of m.

Example

The nthRoot function takes m, n, and an 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 high and low is greater than the tolerance. In each iteration, the midpoint (mid) is calculated. The guess variable stores mid raised to the power of n. Depending on whether guess is greater or smaller than m, the low or high value is updated to narrow down the search range. 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);

Output

The following is the console output −

3.000040054321289

Conclusion

In denouement, the process of calculating the value of (m) to the power 1/n in JavaScript presents an intriguing computational challenge that can be elegantly solved through the implementation of the appropriate algorithm. This mathematical operation, although less commonly encountered, holds significance in various domains such as cryptography, scientific modeling, and data analysis. By harnessing the power of JavaScript and employing a precise methodology, programmers can effectively compute this expression, unlocking new possibilities and empowering the development of sophisticated applications. In conclusion, mastering the calculation of (m) to the power 1/n in JavaScript expands the repertoire of mathematical operations at programmers' disposal, fostering innovation and enabling the realization of complex mathematical concepts in the realm of web development.

Updated on: 04-Aug-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements