Reference · Math

math.isqrt

signature
math.isqrt(int) -> int

What it does

The integer square root: the largest int whose square is not above n, as Python's math.isqrt. math.isqrt(17) is 4. It is exact for every int, where math.floor(math.sqrt(n)) can be off by one for ints past 2⁵³.

Examples

print(math.isqrt(0), math.isqrt(1), math.isqrt(15), math.isqrt(16), math.isqrt(17))print(type_of(math.isqrt(17)), math.sqrt(17))
output
0 1 3 4 4int 4.123105625617661

Edge cases

print(math.isqrt(9223372036854775807))let square = (n) => math.isqrt(n) * math.isqrt(n) == nprint(square(144), square(145), math.isqrt(16.0))
output
3037000499true false 4

Errors

A negative number is a runtime error, not an error value, with a message that says so, as Python's ValueError. A fraction, text or null is a runtime error as well.

this one fails
print(math.isqrt(-1))
output
line 1: `math.isqrt` is not defined for negative numbers (-1)
this one fails
print(math.isqrt(2.5))
output
line 1: `math.isqrt` expects an int as argument 1, got float

Chaining

In a pipeline the number comes first:

print(1000000 | math.isqrt)print([1, 2, 3, 4, 5] | map(math.isqrt))
output
1000[1, 1, 1, 2, 2]

See also

math.sqrt, math.floor, math.gcd

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On math.isqrt(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo