Python Program for Find cubic root of a number
Last Updated :
27 Jul, 2023
Given a number n, find the cube root of n.
Examples:
Input: n = 3
Output: Cubic Root is 1.442250Input: n = 8
Output: Cubic Root is 2.000000
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))
OutputCubic 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))
OutputCubic 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))
OutputCubic 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))
OutputCubic 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))
OutputCubic 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:
- Initialize x to n.
- Compute y as (2x + n/x^2) / 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.
- 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))
OutputCubic 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).
Similar Reads
Python Program to Find Cube of a Number We are given a number and our task is to find the cube of this number in Python. The cube of a number is calculated by multiplying the number by itself twice. For example, if the input is 3, then the output will be 3 * 3 * 3 = 27. In this article, we will learn different ways to find the cube of a n
2 min read
Python program to find power of a number The task of finding the power of a number in Python involves calculating the result of raising a base number to an exponent. For example, if we have a base 2 and an exponent 3, the result is 2^3=8 .Using ** operatorThis is the simplest and most Pythonic way to calculate the power of a number. The **
2 min read
Python Program for cube sum of first n natural numbers We are given a number n and we need to print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till the n-th term in python.Examples:Input: n = 5Output: 225Explanation: 13 + 23 + 33 + 43 + 53 = 225Let's discuss some of the ways to do it.Using Mathematical Formula: Most efficient solution is to use t
2 min read
Python program to calculate square of a given number The task of calculating the square of a number in Python involves determining the result of multiplying a number by itself. For example, given the number 4, its square is 16 because 4 Ã 4 = 16.Using ** operatorexponentiation operator (**) is the most direct and optimized way to compute powers. Since
1 min read
Find Square Root Of Given Number - Python Given an integer X, find its square root. If X is not a perfect square, then return floor(âx). For example, if X = 11, the output should be 3, as it is the largest integer less than or equal to the square root of 11.Using built-in functionsWe can also find the floor of the square root using Pythonâs
3 min read