# Extract Roots in Python

By extracting a root from a number, we most often mean finding the solution to the equation x to the power of n = value; for a square root, the number n is two; for a cubic root, it is three. More often than not, the result and the number refer to real numbers. Root finding is used a lot in programming. Let’s discover how and what methods can be used to effectively extract roots from numbers. First, we will look at what methods are available in Python and determine the most effective. Then, we will consider in more detail how to find not only the square root of a number, but also the cube root, and then the root of n degree.

## Methods of Root Extraction

There are three ways to extract roots in the Python 3 programming language:

• Using the sqrt function from the standard math library math.
• Exponentiation Operation **
• Using the pow(x, n) function

To use the first method, you must first import sqrt from the math module. This is done using the import keyword: `from math import sqrt`. You can use this function to extract only the square root of a number. Here is an example:

```from math import sqrt
x = sqrt(4)
print(x)

2.0```

If we want to calculate the square root of the sum of squares in Python, we can use the hypot function from the math module. We take the sum of the squares of the arguments of the function, and use it to get the root. The function has two arguments.

```from math import hypot
x = hypot(4,3)
print(x)

5.0```

Another method that is a bit more versatile is to use exponentiation. It is known that in order to take the root of n from a number, you must raise it to the power of 1/n. Accordingly, the extraction of the square root of number 4 will look like this:

```n = 2
x = 4**(1./n)
print(x)

2.0```
Note that in Python 2 you have to put a dot after the one, otherwise an integer division will happen and 1/n == 0, not the fraction we need. In Python 3, you don’t have to put a period.

The last method uses the pow(value, n) function. This function will take as its argument value the number to be raised to a power, and the second argument will be responsible for the power of the number. As in the previous method, you must use a fraction in order to get the root of the number.

```x = pow(4, 0.5)
print(x)

2.0```

### Which method is faster?

To find out which method is better to use, let’s write a program. We will measure the execution time using the monotonic method of the time library.

```from time import monotonic
from math import sqrt
iterations = 1000000
start = monotonic()
for a in range(iterations):
x = sqrt(4)
print("sqrt time: {:>.3f}".format(monotonic() - start) + " seconds")
start = monotonic()
for a in range(iterations):
x = 4 ** 0.5
print("** time: {:>.3f}".format(monotonic() - start) + " seconds")
start = monotonic()
for a in range(iterations):
x = pow(4, 0.5)
print("pow time: {:>.3f}".format(monotonic() - start) + " seconds")

sqrt time: 0.266 seconds
** time: 0.109 seconds
pow time: 0.453 seconds```

As you can see, the fastest solution is to use **. In second place is the sqrt method, while pow is the slowest. However, the sqrt method is most obvious when calculating square roots in Python.

So, if speed is critical, we use **. If speed is not the issue, but readability is, we recommend using sqrt.

## Square Root

The most obvious, but not the fastest, way to extract the square root is to use sqrt from the math module.

```from math import sqrt
x = sqrt (value)```

But you can also use tricks involving multiplication by powers of 1/2, which also give the right result. `x = value ** (0.5)`, or `x = pow(value, 0.5)`.

## Cubic Root

The sqrt method won’t work in Python 3 for extracting the cube root, so use exponentiation to 1/3: `x = value ** (1./3)` or `x=pow(value, 1/3)`.

## Root of an n-step

The n-degree root of a number in Python can be extracted in two ways using the 1.0/n exponentiation method:

• Using the ** operator.
• Using the pow function.

As tested above, the ** operator is faster. Therefore, it is more appropriate to use it. Here is an example of calculating cube roots in Python 3 using these two methods:

```n = 4.
x = 16.0 ** (1./n)
print(x)
x = pow(16.0, 1./n)
print(x)

2.0
2.0```

## The root of a negative number

Let’s see how functions behave if we take the root of a negative number.

```from math import sqrt
x = sqrt(-4)

File "main.py", line 2, in
x = sqrt(-4)
ValueError: math domain error```

As you can see, the sqrt function throws an exception. Let’s now see what happens when we use other methods.

```x = -4 ** 0.5
print(x)
x = pow(-4, 0.5)
print(x)

-2.0
(1.2246467991473532e-16+2j)```

As can be seen from the result, the ** operator does not generate an exception and returns an incorrect result. The pow function works correctly. As a result, we get the complex number 2j, which is correct.

## Output

In Python, there are two universal ways to extract the root of a number. One is to raise it to the required power of 1/n. You can also use a function from the math module of the language if you want to extract the square root of a number. All of these methods have their advantages and disadvantages. The most elegant one is sqrt, but only applies to square roots of a number. The other methods are not as elegant, but can easily extract the square root of a number. In addition, the ** operator was the fastest in the test. We must also remember about integer division, the wrong use of which can lead to calculation errors.

You Might Be Interested In