- Reason
- 4 * 4 = 16
Check Perfect Square in Python
What you’ll learn
- What a perfect square means with easy examples.
- How to check a number using an integer loop.
- How to use square root and list perfect squares in a range.
Overview
A perfect square is any number that can be written as k * k. This page shows two checking styles and a live preview.
Two programs
Loop check and square-root-based range scan.
Live preview
Try values like 15, 16, 1, and 0 instantly.
Interview clarity
Know both integer and root-based methods.
Prerequisites
Basic Python loops, integer multiplication, and import usage.
- For-loop and if-condition basics.
- Knowing that
math.isqrtgives integer square root.
What is a perfect square?
A perfect square is a number equal to k squared for some integer k.
Examples: 1, 4, 9, 16, 25. Non-examples: 2, 3, 5, 6.
Quick examples
- Reason
- Between 3^2 and 4^2
- Reason
- 1 * 1 = 1
Live preview
Checks with integer logic, then reports verdict.
Algorithm
Goal: verify if n equals k squared for some integer k.
Loop method
Try i from 0 while i*i <= n and look for exact match.
Square-root method
Use integer root and compare root*root with n.
📜 Pseudocode
function is_perfect_square_loop(n):
if n < 0:
return false
i = 0
while i * i <= n:
if i * i == n:
return true
i = i + 1
return false Integer loop check
Simple and beginner-friendly perfect square check.
def is_perfect_square(number: int) -> bool:
if number < 0:
return False
i = 0
while i * i <= number:
if i * i == number:
return True
i += 1
return False
test_number = 16
if is_perfect_square(test_number):
print(f"{test_number} is a perfect square.")
else:
print(f"{test_number} is not a perfect square.") Range scan using integer square root
Use math.isqrt and print all perfect squares from 1 to 50.
import math
def is_perfect_square(num: int) -> bool:
if num < 0:
return False
root = math.isqrt(num)
return root * root == num
print("Perfect Squares in the Range 1 to 50:")
for i in range(1, 51):
if is_perfect_square(i):
print(i, end=" ") Notes
Prefer integer roots. math.isqrt avoids floating-point rounding issues.
Loop complexity. Loop approach takes O(sqrt(n)) checks.
Negative values. Return false immediately for negatives.
❓ FAQ
🔄 Input / output examples
Try these values in the first example.
| Value | Typical result |
|---|---|
| 16 | Perfect square |
| 15 | Not perfect square |
| 1 | Perfect square |
| 0 | Perfect square |
Edge cases
Zero is square
0 = 0 * 0.
Not a real integer square
Return false for negatives in this tutorial.
Avoid float precision pitfalls
Prefer math.isqrt over float sqrt for reliability.
⏱️ Time and space complexity
| Approach | Time (single n) | Extra space |
|---|---|---|
| Loop until i*i > n | O(sqrt(n)) | O(1) |
| isqrt-based check | O(1) practical | O(1) |
| Range 1..U scan | O(U) checks | O(1) |
Summary
- Definition: n is square if n = k * k.
- Methods: integer loop or
math.isqrt. - Remember: 0 and 1 are perfect squares.
A perfect square can be arranged into a square grid with equal rows and columns. The gaps between consecutive squares (1, 4, 9, 16...) are odd numbers (3, 5, 7, 9...).
9 people found this page helpful
