In Python, the easiest way we can find the nth root of a number is to use the pow() function from the Python math module.
import math
n = 3
cube_root_of_10 = math.pow(10,1/n) #nth root of 10 where n = 3
You can also use the built in ** operator to find the nth root of a number.
n = 5
fifth_root_of_10 = 10**(1/n)
The Python math module has many powerful functions which make performing certain calculations in Python very easy.
One such calculation which is very easy to perform in Python is finding the nth root of a number.
The pow() function from the Python math module also lets us compute roots.
The Python power function pow() takes two numbers as input, the first number is the base and the second number is the exponent. The first number must be positive, but the second number can be negative.
For the nth root of a number, we pass “1/n” to the second parameter in the pow() function.
Below are some examples of how to use the pow() function to find different roots of a number.
For example, if we want to find the square root of a number in Python, we pass “1/2” to the pow() function.
import math
print(math.pow(4, 1/2))
print(math.pow(9, 1/2))
print(math.pow(13, 1/2))
print(math.pow(90, 1/2))
print(math.pow(2182, 1/2))
#Output:
2.0
3.0
3.605551275463989
9.486832980505138
46.71188285650665
For example, if we want to find the square root of a number in Python, we pass “1/3” to the pow() function.
import math
print(math.pow(4, 1/3))
print(math.pow(9, 1/3))
print(math.pow(13, 1/3))
print(math.pow(90, 1/3))
print(math.pow(2182, 1/3))
#Output:
1.5874010519681994
2.080083823051904
2.3513346877207573
4.481404746557164
12.970346612351785
In general, to find the nth root of a number in Python, pass “1/n”. For example, to get the fifth root of a number, we pass “1/5” to the second parameter of pow().
import math
print(math.pow(124, 1/5))
#Output:
2.622311847181126
Finding the nth Root of a Number with the ** Operator in Python
We can also use the built in ** to perform exponentiation in Python. To find a nth root with the ** operator, we just put “(1/n)” after **.
Unlike the pow() function, we can find the nth root of negative numbers with the ** operator if n is odd.
Below are some examples of how to use the Python built in ** operator to find nth roots.
import math
#n=2
print(4**(1/2))
#n=3
print(9**(1/3))
#n=5
print(-13**(1/5))
#n=6
print(90**(1/6))
#n=9
print(-2182**(1/9))
#Output:
2.0
2.080083823051904
-1.6702776523348104
2.1169328630254585
-2.3495455051249885
Hopefully this article has been beneficial for you to learn how to find the nth root of a number in Python.