Python math.isqrt() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.8+

Definition

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)
CallWhat 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

Parameters

ParameterDescription
nRequired positional non-negative integer (int). Floats raise TypeError; negative ints raise ValueError.

Return Value

Return ValueDescription
intExact integer floor of the square root of n.

Python Examples

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}")

How It Works

  1. Floor root as side. math.isqrt(12) finds 3 because 3x3 uses 9 tiles while 4x4 would need 16.
  2. Exact remainder. Subtracting side * side stays purely in integer arithmetic.
  3. Zero leftover for squares. Counts of 16 and 25 tile evenly with zero remaining.

Example 2 — Stay Exact Past 53 Bits Where float Fails

Test (10 ** 18) ** 2 - 1 where int(math.sqrt(n)) rounds up by mistake while math.isqrt(n) is exact.

python
import math

big = (10 ** 18) ** 2 - 1
exact = math.isqrt(big)
rounded = int(math.sqrt(big))

print("isqrt:", exact)
print("sqrt :", rounded)
print("exact squared <= big:", exact * exact <= big)

How It Works

  1. Just under a huge square. Subtracting one from 10**36 leaves a 36-digit non-square.
  2. Float loses precision. Converting big to float rounds it to 1e36, so math.sqrt() reports 10**18.
  3. 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}")

How It Works

  1. Guard negatives first. Negative integers cannot be real squares, so the helper immediately returns False.
  2. Exact product test. Multiplying r * r verifies whether n is an exact square.
  3. 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.

Next: math.issubnormal()

Return True if x is a nonzero subnormal (denormal) float.

issubnormal() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful