Find Cube root of a number using Log function - GeeksforGeeks

17 Mar, 2021
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) = d Now, 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++
Java
Python3
C#
Javascript
// C++ program to Find Cube root// of a number using Logarithm #include <bits/stdc++.h> // Function to find the cube rootdouble 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 codeint main(){    double N = 8;     printf("%.2lf ", cubeRoot(N));     return 0;}
// Java program to Find Cube root// of a number using Logarithmclass GFG{     // Function to find the cube rootstatic 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 codepublic static void main(String[] args){    double N = 8;    System.out.printf("%.2f", cubeRoot(N));}} // This code is contributed by Rajput-Ji
# Python3 program to find cube root# of a number using logarithmimport numpy as np # Function to find the cube rootdef 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 codeN = 8 print("%.2f" % cubeRoot(N)) # This code is contributed by PratikBasu
// C# program to find cube root// of a number using logarithmusing System; class GFG{     // Function to find the cube rootstatic 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 codepublic static void Main(String[] args){    double N = 8;         Console.Write("{0:F2}", cubeRoot(N));}} // This code is contributed by sapnasingh4991
<script>// javascript  program to Find Cube root// of a number using Logarithm  // Function to find the cube rootfunction cubeRoot( n){     // calculate the cube root    let ans = Math.pow(3, (1.0 / 3)                            * (Math.log(n) / Math.log(3)));     // Return the final answer    return ans;} // Driver codelet N = 8;document.write( cubeRoot(N).toFixed(2));     // This code is contributed by todaysgaurav </script>
2.00
 
Rajput-Ji
PratikBasu
sapnasingh4991
todaysgaurav
maths-cube
root
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java