# Fifth root of a number in C++

In this problem, we are given a number N. Our task is to find the floor value of the fifth root of a number.

Fifth Root of a number is the number which when multiplied to itself 5 times returns the number.

If N1/5 = a then, a*a*a*a*a = N.

## Let’s take an example to understand the problem,

Input: N = 325

Output: 3

Explanation:

The fifth root of 325 is 3.179 whose floor value is 3.

Solution Approach:

A simple solution to the problem is traversing from 1 to n. And finding the number which when multiplied to itself five times gives the number.

Here, the exact value cannot be found as the numbers will not always be perfect fifth power. So, we will find the first value that makes the fifth power more than n and then return the value -1 to get the floor fifth root.

## Example

Live Demo

#include<iostream>
using namespace std;

int calcFifthRoot(int n) {

if (n == 0 || n == 1)
return n;

int a = 0;
for(a = 1; a*a*a*a*a < n ; a++){

}
return (a - 1);
}

int main() {

int n = 325;
cout<<"The Floor of fifth root of "<<n<<" is "<<calcFifthRoot(n);
return 0;
}

## Output −

The Floor of fifth root of 325 is 3

This algorithm is good but there can be a more promising solution to the problem. This can be done by updating the search algorithm and using the binary Search algorithm for searching for the fifth root of the number.

## Example

Live Demo

#include<iostream>
using namespace std;

int calcFifthRoot(int n)
{
if (n == 0 || n == 1)
return n;

int start = 1, end = n, root = 0;
while (start <= end)
{
int a = (start + end) / 2;
long int apowfive = a*a*a*a*a;

if (apowfive == n)
return a;
if (apowfive < n) {

start = a + 1;
root = a;
}
else
end = a - 1;
}
return root;
}

int main() {

int n = 250;
cout<<"The floor of fifth root of "<<n<<" is "<<calcFifthRoot(n);
return 0;
}

## Output −

The floor of fifth root of 250 is 3

Updated on: 22-Jan-2021

713 Views