Definition
n = k²
Some integer k squares to n.
A perfect square equals k * k for some whole number k. Examples: 1, 4, 9, 16, 25. This tutorial covers the loop and math.isqrt approaches, a live checker, worked Python examples, edge cases, and complexity.
n = k²
Some integer k squares to n.
Beginner
Try candidates while i*i <= n.
Robust
Integer root, then root*root == n.
Both square
0*0 and 1*1 both count.
Try 16 / 15
See k and the verdict instantly.
Different idea
Squares vs divisor sums.
A perfect square is a non-negative integer that equals some integer squared. So 16 is perfect because 4 * 4 = 16, while 15 is not because no whole k works.
Interviews usually accept either a clear i * i loop or a math.isqrt check. Prefer integer roots over floating sqrt so large values stay exact.
It is a classic math interview warm-up that teaches exact integer reasoning without float traps.
Some integer k squares to n.
Loop or math.isqrt.
Both are perfect squares.
isqrt beats float sqrt.
In short: find whether some integer k satisfies k * k == n.
Given an integer n, decide whether it is a perfect square of a non-negative integer.
# 16 -> 4 * 4 = 16 perfect
# 15 -> no integer k not perfect
# 0 -> 0 * 0 = 0 perfect
# 1 -> 1 * 1 = 1 perfect | Item | Type | Description |
|---|---|---|
n / number | int | Value to test (non-negative for yes). |
| Return | bool | True when some k has k*k == n. |
| Optional k | int | The integer root when the answer is yes. |
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 | Method | Idea | Notes |
|---|---|---|
| i*i loop | Try candidates until square exceeds n | Clearest for beginners |
| math.isqrt | root = isqrt(n); root*root == n | Fast and exact for integers |
| float sqrt | round(sqrt(n))**2 == n | Risky for large n — avoid |
| Goal | Pattern |
|---|---|
| Reject negatives | if n < 0: return False |
| Loop check | while i * i <= n: |
| Exact hit | if i * i == n: return True |
| isqrt check | root = math.isqrt(n) |
| Verify root | return root * root == n |
| Build squares | k * k for k = 0, 1, 2, … |
Same question — different reliability.
while i*i <= nInterview-friendly and exact
root*root == nPreferred production check
avoid for intsRounding can lie on big n
k*k vs s(n)=nDifferent “perfect” meaning
Reach for a square check whenever you need exact integer roots.
Simple math with an exactness twist.
Can n form a square layout?
Keep only square values in a range.
Show why integer roots beat floats.
This tutorial targets integer n.
Key benefit: one crisp boolean question that forces you to think in exact integers, not approximate roots.
Checks with integer logic, then reports the root and verdict.
Three complete Python programs — loop check for 16, list squares from 1 to 50 with math.isqrt, and generate squares by squaring. Click View Output to reveal sample console results.
A beginner-friendly loop that never needs floating roots.
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.") Candidates advance from 0 while i * i has not passed 16. When i reaches 4, the product matches and the function returns True.
Use the standard library for a crisp, exact check.
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=" ") math.isqrt(num) returns the floor of the square root. Squaring that root recovers num exactly when num is a perfect square.
Build squares directly instead of filtering every integer.
print("First squares from k = 0 to 7:")
for k in range(0, 8):
square = k * k
print(f"{k} * {k} = {square}") When you only need the square sequence, squaring consecutive integers is cheaper than testing every n in a range.
No non-negative integer squares to a negative.
Loop i while i*i <= n, or call isqrt(n).
Exact match means perfect square.
True with root k, or False.
Trace the loop method for n = 16.
| i | i * i | i*i <= 16? | Match? |
|---|---|---|---|
0 | 0 | Yes | No |
1 | 1 | Yes | No |
2 | 4 | Yes | No |
3 | 9 | Yes | No |
4 | 16 | Yes | Yes — return True |
4 * 4 equals 16 — perfect square.
Where perfect-square checks show up beyond the interview prompt.
Exact integer math checks.
Example: is_square(16).
List squares inside a band.
Example: 1..50 list.
Build squares with k*k.
Example: Example 3.
Can n tiles form a square?
Example: 25 -> 5×5.
Show exact integer roots.
Example: avoid float sqrt.
Continue the interview chain.
Example: related CTA.
Pro Tip: say “I’ll check whether root*root equals n using an integer root” before coding.
Why these approaches work well for beginners and interviews.
Dry-run 16 on paper and watch i grow.
No float rounding surprises with isqrt.
Loop for clarity; isqrt for speed.
k*k builds the sequence without scanning.
Pro Tip: lead with the loop in interviews, then mention math.isqrt as the robust alternative.
Small habits that keep square checks interview-ready.
Return False immediately for n < 0.
Exact integer root for production code.
Never trust a root without squaring back.
Use k*k if you need the sequence itself.
Name the definition so interviewers know you know.
Pro Tip: sanity-check 0, 1, 16, and 15 — if those four behave, your logic is solid.
Mistakes that commonly break perfect-square programs.
Large ints can round incorrectly.
→ Use math.isqrt or an integer loop.
Taking floor(sqrt) without squaring back.
→ Always compare root * root to n.
Different “perfect” concept entirely.
→ This page is about k * k.
Starting i at 1 and rejecting zero.
→ 0 = 0 * 0 is a square.
Returning True for -16 in real-integer checks.
→ Reject n < 0 in this tutorial.
Handle these before claiming the check is complete.
0 = 0 * 0.
1 = 1 * 1.
Return false for negatives in this tutorial.
Prefer math.isqrt over float sqrt.
Between 9 and 16 — not square.
4 * 4 = 16.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
math.isqrt.math.isqrt over float sqrt. The loop approach takes O(sqrt(n)) checks. Return false immediately for negatives.Quick Takeaway: n is a perfect square when some integer k satisfies k * k == n.
| 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) |
For interview demos, either method is fine; mention float pitfalls when asked about reliability.
A perfect square equals some integer squared. Use an i * i loop or math.isqrt, reject negatives, and remember that 0 and 1 count.
Practice the three examples above, then continue to finding the average of N numbers.
n = k² means perfect square; verify with integers, not float sqrt.
Decide exact squares the interview-friendly way.
n = k * k
Definitionwhile i*i
Methodroot*root
Robust0, 1 yes
GuardsO(√n)
AnalysisA 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...).
Learn how to find the average of N numbers in Python.
9 people found this page helpful