Check if product of Array elements in given range are M-th root or not


M-th root is defined as the cube of any number and array ranges mean to count the indexes from first to end. We will take three numbers in the array range as input and see if their product value comes in the form of a cube value then it will be the 'M-th' root of the number.

Let’s take an example to understand the product range of an array and calculate the M-th root of the number.

Example 1

Given array integer is 9, 8, 3, 1

Now we see the product range of array 9*8*3*1 is 216.

Therefore, 216 verifies as M-th root number and the number 216 is a cube of 6.

Example 2

Given array integer is 6, 5, 8

Now we see the product range of array 6*5*8 is 240.

Therefore, 240 is not verified as M-th root number and the number 240 is not the cube of any number.

In this article, we are going to solve the array elements in a given range are M-th root or not.

Syntax

pow( base_value, power_value )

This method takes two parameters one for base value and the second for power value. For example- pow( 4, 3.0 ) represents 4 raised to the power 3.0 and the result becomes 64.

round()

This method returns the rounded value in the form of an integer value.

Algorithm

  • We will start the program by using header files namely ‘iostream’ and ‘cmath’.

  • Now we start with the main function and initialize the array value to ‘arr[]’ variable and declare variable ‘n’ to store the size of the ‘arr[]’ variable. Both these variables will be used to find the M-th root of the number.

  • Declare variable ‘m’ to ‘3’ as the representation of the power to the base value and declare product value to ‘1’ to multiply the product of first two elements of the array with the next element in the array and so on.

  • We are using for loop to iterate the array element and multiply all the array elements to store it into the ‘prod’ variable.

  • After that we are declaring ‘find_base’ variable of the double data type to find the base of the number by using the predefined function pow().

  • To make the rounded value we use the predefined function round() to the ‘base_value’ variable.

  • Moving ahead we will create an if-else statement to check the similarity between ‘pow ( base_value, m)’ and ‘prod’. This similarity will verify if the product of the numbers is equal to the cube root of the given number or not and print the output accordingly.

Example

In this program, we are going to execute the product of array elements in a given range are M-th root or not.

#include <iostream>
#include <cmath> 
using namespace std;
int main () {
   int arr[] = { 18, 2, 6 };
   int n = sizeof (arr) / sizeof (arr[0]);
   int m = 3 
   // represents the power of base value

   int prod = 1; 
   // it will mutiple to arr[i]
   for (int i = 0; i < n; i++) {
      prod *= arr[i]; 
      // mutiply all the array element and store to prod (18*2*6 = 216)
   }
   double find_base = pow (prod, 1.0 / m);
   int base_value = round (find_base); 
   // output = 6

   if (pow ( base_value , m) == prod)
   // pow( 6 , 3 ) == 216 and 6 power 3 is 216. 
   {
      cout << prod << " , is a perfect " << m << "-th power of " << base_value <<" and Mth root of the number."<<endl;
   }
   else
   {
      cout << prod << " , This is not a perfect " << m << "-th power" << base_value <<" and not the Mth root of the number."<< endl;
   }
   return 0;
}

Output

216 , is a perfect 3-th power of 6 and this is Mth root of the number.

Conclusion

We explored the concept of product ranges of the array to verify whether the number is the M-th root or not. We saw how different forms of other multiples of an array range comes in the form of a cube and also saw the difference of range() and pow() functions in this program. The real example of a cube root is finding the edge of a cube.

Updated on: 10-May-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements