What's a good algorithm to determine if an input is a perfect square?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A perfect square is an integer that can be written as k * k for some integer k. A good algorithm for detecting one depends on the language and the size of the input, but the best general-purpose answer for ordinary programming is to compute an integer square root and then square it back to verify the result.
The Cleanest Modern Approach
In Python, math.isqrt gives you the integer square root directly. That avoids floating-point accuracy problems.
This is usually the best answer because it is:
- fast,
- exact for integers,
- and simple to reason about.
The idea is straightforward: if root is the floor of the square root, then only one multiplication is needed to confirm whether n is a perfect square.
Why Floating-Point Square Roots Are Riskier
A tempting approach is to do this:
That works for many small values, but it relies on floating-point representation. For very large integers, floating-point rounding can make this approach less trustworthy than integer arithmetic.
So while it is mathematically intuitive, it is not the strongest algorithmic choice when exactness matters.
Binary Search Is a Good Language-Agnostic Alternative
If your language does not offer an integer square root helper, binary search works well.
This runs in O(log n) iterations and uses only integer operations. It is a strong fallback when you want a portable algorithm independent of special library support.
Handle Edge Cases Explicitly
There are a few cases worth deciding up front:
- negative numbers are not perfect squares in ordinary integer arithmetic,
- '
0is a perfect square,' - '
1is a perfect square.'
Those cases are easy to support, but it is worth stating them because many implementations forget the negative-input rule or overcomplicate the 0 and 1 cases.
Performance Perspective
For most applications, the integer square root approach is already excellent. You usually do not need something more exotic. The choice is mostly about correctness and clarity:
- '
math.isqrtwhen available,' - binary search when you need a generic integer-only algorithm,
- floating-point square roots only when you are comfortable with their limitations.
The real mistake is optimizing the check before deciding whether exact integer correctness matters.
Common Pitfalls
- Using floating-point
sqrtand assuming it is always exact for very large integers. - Forgetting that negative integers are not perfect squares.
- Writing a more complicated algorithm when integer square root already solves the problem cleanly.
- Failing to verify the candidate root by squaring it again.
- Treating
0and1as special cases incorrectly.
Summary
- A good perfect-square test uses integer arithmetic instead of floating-point comparisons.
- In Python,
math.isqrtis usually the best tool for the job. - The check is simply: compute the integer root, then test whether
root * root == n. - Binary search is a good fallback in languages without an integer square root helper.
- Negative numbers are not perfect squares, while
0and1are.

