Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Program for Find cubic root of a number

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 09 Mar, 2023
Improve Article
Save Article

Given a number n, find the cube root of n.
Examples: 

Input:  n = 3
Output: Cubic Root is 1.442250

Input: n = 8
Output: Cubic Root is 2.000000
Recommended Practice

We can use binary search. First we define error e. Let us say 0.0000001 in our case. The main steps of our algorithm for calculating the cubic root of a number n are: 

  • Initialize start = 0 and end = n
  • Calculate mid = (start + end)/2
  • Check if the absolute value of (n – mid*mid*mid) < e. If this condition holds true then mid is our answer so return mid. 
  • If (mid*mid*mid)>n then set end=mid
  • If (mid*mid*mid)

Below is the implementation of above idea. 

Python3




# Python 3 program to find cubic root
# of a number using Binary Search
 
# Returns the absolute value of
# n-mid*mid*mid
 
 
def diff(n, mid):
    if (n > (mid * mid * mid)):
        return (n - (mid * mid * mid))
    else:
        return ((mid * mid * mid) - n)
 
# Returns cube root of a no n
 
 
def cubicRoot(n):
 
    # Set start and end for binary
    # search
    start = 0
    end = n
 
    # Set precision
    e = 0.0000001
    while (True):
 
        mid = (start + end) / 2
        error = diff(n, mid)
 
        # If error is less than e
        # then mid is our answer
        # so return mid
        if (error <= e):
            return mid
 
        # If mid*mid*mid is greater
        # than n set end = mid
        if ((mid * mid * mid) > n):
            end = mid
 
        # If mid*mid*mid is less
        # than n set start = mid
        else:
            start = mid
 
 
# Driver code
n = 3
print("Cubic root of", n, "is",
      round(cubicRoot(n), 6))

Output

Cubic root of 3 is 1.44225

Time Complexity: O(logn)
Auxiliary Space: O(1) Please refer complete article on Find cubic root of a number for more details!

Method #2: Using power(**) function

Python3




# Python 3 program to find cubic root of a number
# Driver code
n = 3
print("Cubic root of", n, "is",
      round(n**(1/3), 6))

Output

Cubic root of 3 is 1.44225

Method #3 : Using math.pow()

Python3




# Python 3 program to find cubic root  of a number
# Driver code
import math
n = 3
a = math.pow(n, (1/3))
print("Cubic root of", n, "is", round(a, 6))

Output

Cubic root of 3 is 1.44225

Method 4: Using the exponentiation operator in the math library

Python3




import math
 
 
def cubic_root(n):
    return round(math.exp(math.log(n)/3), 6)
 
 
n = 3
print("Cubic Root of", n, "is", cubic_root(n))
 
n = 8
print("Cubic Root of", n, "is", cubic_root(n))

Output

Cubic Root of 3 is 1.44225
Cubic Root of 8 is 2.0

Time complexity: O(log n) where n is the input number
Auxiliary space: O(1), as the algorithm only uses a few variables.

Method 5: without using the power function or the exponentiation operator in the math library:

This method uses the iterative method of computing the cubic root of a number. The idea is to start with an initial guess and improve the guess iteratively until the difference between the guess and the actual cubic root is small enough. This method is known as the Babylonian method or Heron’s method.

Python3




def cubic_root(n):
    # Initialize the variables
    x = n
    y = (2 * x + n / x**2) / 3
 
    # Iterate until convergence
    while abs(x - y) >= 0.000001:
        x = y
        y = (2 * x + n / x**2) / 3
 
    return y
 
# Driver code
n = 3
print("Cubic root of", n, "is", round(cubic_root(n), 6))

Output

Cubic root of 3 is 1.44225

Time complexity: O(log(n))
Auxiliary space: O(1) since we are only using a few variables to store the intermediate values.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!