math.isqrt(n) returns the integer square root of a non-negative integer n: the largest integer r such that r * r <= n.
Added in Python 3.8, it works directly on arbitrary-precision integers without converting to float, so it stays 100% exact even on integers with dozens or thousands of digits.
math.isqrt() is a function in Python's standard math module: import it with import math and call math.isqrt(...), or import the name directly with from math import isqrt. Added in Python 3.8.
Syntax
Import math and pass a non-negative int (Python 3.8+).
python
import math
root = math.isqrt(n)
Call
What it does
math.isqrt(17)
Returns 4 (since 4*4 <= 17 < 5*5).
math.isqrt(25)
Returns 5 (an int, not 5.0).
math.isqrt(-1)
Raises ValueError: argument must be non-negative.
math.isqrt(9.0)
Raises TypeError: floats are not accepted.
Use math.sqrt() when you want fractional decimals or float inputs.
Function signatures
python
math.isqrt(n, /)
# added in Python 3.8; returns floor(sqrt(n)) as int
Example 1 — Build the Largest Square From Available Floor Tiles
Find the side length of the largest square patio and how many tiles remain unused.
python
import math
for tiles in [12, 16, 25, 30]:
side = math.isqrt(tiles)
leftover = tiles - side * side
print(f"tiles={tiles} -> {side}x{side} square, leftover={leftover}")
Just under a huge square. Subtracting one from 10**36 leaves a 36-digit non-square.
Float loses precision. Converting big to float rounds it to 1e36, so math.sqrt() reports 10**18.
Integer math wins.math.isqrt() computes the true 18-nine floor root 999999999999999999.
Example 3 — Write an Exact Perfect-Square Check and Catch Type Errors
Check perfect squares without floats and handle negative or float inputs cleanly.
python
import math
def is_square(n):
if n < 0:
return False
r = math.isqrt(n)
return r * r == n
print(is_square(49), is_square(50), is_square(-4))
for bad_arg in [-1, 9.0]:
try:
math.isqrt(bad_arg)
except (ValueError, TypeError):
print(f"rejected: {bad_arg!r}")
Output
True False False
rejected: -1
rejected: 9.0
How It Works
Guard negatives first. Negative integers cannot be real squares, so the helper immediately returns False.
Exact product test. Multiplying r * r verifies whether n is an exact square.
Strict argument types. Both negative integer -1 (ValueError) and float 9.0 (TypeError) are rejected.
Frequently Asked Questions
math.isqrt() was added in Python 3.8.
math.isqrt() stays in integer math and is always exact. int(math.sqrt(n)) converts n to a 64-bit float first, giving wrong answers above 2**53 or overflowing above 1.8e308.
No. Passing any float raises TypeError because isqrt() works only on integers.
Compute r = math.isqrt(n) after checking n >= 0, then return r * r == n.
It raises ValueError because negative numbers have no real square root.