Is there a short-hand for nth root of x in Python?

PythonMathOperatorsSquare Root

Python Problem Overview


In maths, if I wish to calculate 3 to the power of 2 then no symbol is required, but I write the 2 small: . In Python this operation seems to be represented by the ** syntax.

>>> 3**2
9

If I want to go the other direction and calculate the 2nd root of 9 then in maths I need to use a symbol: 2√9 = 3

Is there a short-hand symbol in Python, similar to ** that achieves this i.e. 2<symbol>9? Or do I need to use the math module?

Python Solutions


Solution 1 - Python

nth root of x is x^(1/n), so you can do 9**(1/2) to find the 2nd root of 9, for example. In general, you can compute the nth root of x as:

x**(1/n)

Note: In Python 2, you had to do 1/float(n) or 1.0/n so that the result would be a float rather than an int. For more details, see https://stackoverflow.com/questions/9595135/why-does-python-give-the-wrong-answer-for-square-root

Solution 2 - Python

You may also use some logarithms:

Nth root of x:

exp(log(x)/n)

For example:

>>> from math import exp, log
>>> x = 8
>>> n = 3
>>> exp(log(x)/n)
2.0

Solution 3 - Python

Also: x**(n**-1), which is the same but shorter than x**(1/float(n))

Solution 4 - Python

If you prefer to apply this operation functionally rather than with an infix operator (the ** symbol), you can pass the base and exponent as arguments to the pow function:

In [23]: (9**(0.5)) == pow(9, 0.5)
Out[23]: True

I am also fond of finding new uses for this Infix hack in Python although it's more of a fun aside than a heavy-duty solution. But you could effectively make your own personal symbol for this by doing the following:

class Infix:
    def __init__(self, function):
        self.function = function
    def __ror__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __or__(self, other):
        return self.function(other)
    def __rlshift__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __rshift__(self, other):
        return self.function(other)
    def __call__(self, value1, value2):
        return self.function(value1, value2)


root_of = Infix(lambda x,y: y**(1.0/x))

print 2 |root_of| 9
3.0

Solution 5 - Python

There is. It's just ** =)

Any nth root is an exponentiation by 1/n, so to get the square root of 9, you use 9**(1/2) (or 9**0.5) to get the cube root, you use 9 ** (1/3) (which we can't write with a simpler fraction), and to get the nth root, 9 ** (1/n).

Also note that as of Python 3, adding periods to integers to make them a float is no longer necessary. Saying 1/3 works the way you would actually expect it to, giving 0.333... as result, rather than zero. For legacy versions of Python, you'll have to remember to use that period (but also critically wonder why you're using a legacy version of a programming language)

Solution 6 - Python

Basically sqrt(9) is equivalent to 9^.5

>>>9**.5
3.0

Solution 7 - Python

You should do

16**(0.5) #If you print it, you get 4, So you can use this formula.

Solution 8 - Python

def nthrootofm(a,n):
    return pow(a,(1/n))
a=81
n=4
q=nthrootofm(a,n)
print(q)

pow() function takes two parameters .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwhytheqView Question on Stackoverflow
Solution 1 - PythonHari MenonView Answer on Stackoverflow
Solution 2 - PythonIdvarView Answer on Stackoverflow
Solution 3 - PythonNacib NemeView Answer on Stackoverflow
Solution 4 - PythonelyView Answer on Stackoverflow
Solution 5 - PythonMike 'Pomax' KamermansView Answer on Stackoverflow
Solution 6 - PythonIshaanView Answer on Stackoverflow
Solution 7 - PythonPersianGulfView Answer on Stackoverflow
Solution 8 - Pythonravi tanwarView Answer on Stackoverflow