Given number N, the task is to find the cube root using log function.
Examples:
Input: N = 8
Output: 2.000000Input: N = 27
Output: 3.000000
Approach: To solve the problem mentioned above we will use log() function, according to the following formula:
Let cube root of N be d.
=> ∛N = d
=> N(1/3) = dNow, apply log on both sides:
log3 (N(1/3)) = log3 (d)
=> log3 (d) = 1/3 * log3 (N)
=> d = 3(1/3 * log3 (N))
Below is the implementation of above problem:
C++
// C++ program to Find Cube root // of a number using Logarithm #include <bits/stdc++.h> // Function to find the cube root double cubeRoot(double n) { // calculate the cube root double ans = pow(3, (1.0 / 3) * (log(n) / log(3))); // Return the final answer return ans; } // Driver code int main() { double N = 8; printf("%.2lf ", cubeRoot(N)); return 0; } |
Java
// Java program to Find Cube root // of a number using Logarithm class GFG{ // Function to find the cube root static double cubeRoot(double n) { // Calculate the cube root double ans = Math.pow(3, ((1.0 / 3) * (Math.log(n) / Math.log(3)))); // Return the final answer return ans; } // Driver code public static void main(String[] args) { double N = 8; System.out.printf("%.2f", cubeRoot(N)); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to find cube root # of a number using logarithm import numpy as np # Function to find the cube root def cubeRoot(n): # Calculate the cube root ans = pow(3, (1.0 / 3) * (np.log(n) / np.log(3))) # Return the final answer return ans # Driver code N = 8 print("%.2f" % cubeRoot(N)) # This code is contributed by PratikBasu |
C#
// C# program to find cube root // of a number using logarithm using System; class GFG{ // Function to find the cube root static double cubeRoot(double n) { // Calculate the cube root double ans = Math.Pow(3, ((1.0 / 3) * (Math.Log(n) / Math.Log(3)))); // Return the readonly answer return ans; } // Driver code public static void Main(String[] args) { double N = 8; Console.Write("{0:F2}", cubeRoot(N)); } } // This code is contributed by sapnasingh4991 |
2.00
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find minimum number of Log value needed to calculate Log upto N
- Square root of a number using log
- Nth root of a number using log
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- Program to compute log a to any base b (logb a)
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Check if a number can be expressed as power | Set 2 (Using Log)
- Minimum divisor of a number to make the number perfect cube
- Previous perfect square and cube number smaller than number N
- Find square root of number upto given precision using binary search
- Find root of a number using Newton's method
- Square root of a number without using sqrt() function
- Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
- Find the ln(X) and log10X with the help of expansion
- Find Cube Pairs | Set 1 (A n^(2/3) Solution)
- Find Cube Pairs | Set 2 (A n^(1/3) Solution)
- Find the diagonal of the Cube
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

