Functional Weave
Code in Rust

math.integer-sqrt@1.0.0

impl/python.py

483 bytes · the Python implementation · view raw

import math

MAX_SAFE = 2**53 - 1


def integer_sqrt(n: int) -> int:
    """The largest whole number whose square does not exceed ``n``.

    ``math.isqrt`` is exact; the range check keeps the answers the same as the
    TypeScript and Rust ports, which are limited to 2^53 - 1.
    """
    if isinstance(n, bool) or not isinstance(n, int) or n < 0 or n > MAX_SAFE:
        raise ValueError("n must be a whole number from 0 to 2^53 - 1, received %r" % (n,))
    return math.isqrt(n)