Menu Close

How to calculate an integer square root in Python?

Sometimes, we want to calculate an integer square root in Python.

In this article, we’ll look at how to calculate an integer square root in Python.

How to calculate an integer square root in Python?

To calculate an integer square root in Python, we can use the math.isqrt function.

For instance, we write

n = 1000
root = math.isqrt(n)

to call math.isqrt with n to return the integer square root of n.

It’s either the exact square root or the biggest integer such that root squared is less than or equal to n.

isqrt is available since Python 3.8.

Conclusion

To calculate an integer square root in Python, we can use the math.isqrt function.

Posted in Python, Python Answers