Integer square root 2026-10-20
For a nonnegative integer , its integer square root is the unique nonnegative integer satisfying , equivalently . Testing decides whether is a perfect square using exact integer arithmetic.
It performs a trial division test for a prime number. An integer below is rejected; otherwise the function searches for a divisor from through . A composite number has a prime factor no larger than its square root, so finding no such divisor proves primality. For example, mystery2(97) returns True, while mystery2(91) returns False, since .
This describes the intended mathematical test. For extremely large integers, the floating-point computation n**0.5 can round or overflow; an implementation intended for arbitrary-size integers should use an exact integer square root.
Perfect square test 2026-10-20
For a nonnegative integer , compute its integer square root and accept exactly when . An approximate square root must be corrected with exact integer comparisons before this equality is tested. This procedure distinguishes a squareness test from merely approximating a square root.