Open In App

Python Program for Find cubic root of a number

Improve
Improve
Like Article
Like
Save
Share
Report

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

Python Program for Find cubic root of a number

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!

Python Program for Find cubic root of a number 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

Python Program for Find cubic root of a number 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

Find cubic root of a number 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.

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.

Python Program for Find cubic root of a number Using reduce()

Algorithm:

  1. Initialize x to n.
  2. Compute y as (2x + n/x^2) / 3.
  3. While the absolute difference between x and y is greater than or equal to 0.000001, set x to y and compute a new value of y using the same formula as step 2.
  4. Return the final value of y.

Python3




from functools import reduce
 
 
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 = reduce(lambda a, b: (2 * a + n / b**2) / 3, [y, y])
    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: The while loop iterates until the difference between x and y is less than 0.000001. Therefore, the number of iterations required is proportional to log(1/0.000001), which is constant. Thus, the time complexity is O(1).
Auxiliary Space: The algorithm uses only two variables, x and y, which require constant space. Therefore, the space complexity is also O(1).



Last Updated : 27 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads