
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Cube Root of a Given Number in Python
Mathematically, a cube root of a certain number is defined as a value obtained when the number is divided by itself thrice in a row. It is the reverse of a cube value. For example, the cube root of 216 is 6, as 6 Ã 6 Ã 6 = 216. Our task in this article is to find the cube root of a given number using python.
The cube root is represented using the symbol "$\mathrm{\sqrt[3]{a}}$". The 3 in the symbol denotes that the value is divided thrice in order to achieve the cube root.
There are various ways in python to calculate the cube root of a number. Let us look at them one by one below ?
Using a simple math equation.
Using math.pow() function.
Using cbrt() function in numpy.
Input Output Scenarios
Let us now look at some input output scenarios to calculate thee cube root of a given number ?
Assume the given input number is positive, the output is displayed as ?
Input: 8 Result: 2
Assume the given input is negative, the output is displayed as ?
Input: -8 Result: -2
Assume the input is a list of elements, the output is obtained as ?
Input: [8, -125] Result: [2, -5]
Using Mathematical Equation
Let us start simple; we use a simple mathematical equation to find the cube root of a number in python. In here, we find the $\mathrm{\frac{1rd}{3}}$. power of the input number.
Example 1: For Positive Numbers
Given below is a python program that computes the cube root of a positive number.
#take an input number num = 216 #calculate cube root cube_root = num ** (1/3) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
Output
The output of the above python code is ?
Cube root of 216 is 5.999999999999999
Example 2: For Negative Numbers
Given below is a python program that computes the cube root of a negative number.
#take an input number num = -216 #calculate cube root cube_root = -(-num) ** (1/3) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
Output
Cube root of -216 is -5.999999999999999
Using math.pow() Function
The math.pow(x, y) function returns the value of x raised to the power y, with the condition of x being always a positive value. So in this case, we use this function to raise the input number to its $\mathrm{\frac{1rd}{3}}$. power.
Example 1: For Positive Numbers
In the python program below, we find the cube root of a positive input number
import math #take an input number num = 64 #calculate cube root cube_root = math.pow(num, (1/3)) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
Output
The output achieved is ?
Cube root of 64 is 3.9999999999999996
Example 2: For Negative Numbers
In the python program below, we find the cube root of a negative input number.
import math #take an input number num = -64 #calculate cube root cube_root = -math.pow(-num, (1/3)) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
Output
The output achieved is ?
Cube root of -64 is -3.9999999999999996
Using numpy's cbrt() function
cbrt() is a built-in function in the numpy library that returns the cube root of every element present in an array inputted. This method does not raise an error while finding the cube root of a negative number therefore making it more efficient than previous approaches.
Example
In the python example below, we are taking inputs using python lists and using cbrt() function we find the cube root.
#import numpy library to access cbrt() function import numpy as np #take an input list num = [64, -729] #calculate cube root of each element in the list cube_root = np.cbrt(num) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
Output
On compiling and executing the python code above, the output can be obtained as ?
Cube root of [64, -729] is [ 4. -9.]