
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
Program to illustrate the working of our solution,
Example
#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.
Program to illustrate the working of our solution,
Example
#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
- Related Articles
- Find cubic root of a number in C++
- N-th root of a number in C++
- Find the fifth triangular number.
- Primitive root of a prime number n modulo n in C++
- Number of elements smaller than root using preorder traversal of a BST in C++
- How to get Fifth Element of the Tuple in C#?
- Print a number containing K digits with digital root D in C++
- How to calculate square root of a number in Python?
- Get square root of a number using Math.sqrt in Java
- 8086 program to find the square root of a perfect square root number
- Check if a number is perfect square without finding square root in C++
- Find square root of number upto given precision using binary search in C++
- Returning a range or a number that specifies the square root of a number in JavaScript
- C++ Program to calculate the cube root of the given number
- Finding Cube Root of Specified Number in Golang
