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.floor(Math.sqrt(n)) approaches, a live checker, worked JavaScript 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.floor(Math.sqrt(n)) 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.floor(Math.sqrt(n)).
Both are perfect squares.
Math.sqrt with floor beats raw 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 isPerfectSquareLoop(n) {
if (n < 0) {
return false;
}
let i = 0;
while (i * i <= n) {
if (i * i === n) {
return true;
}
i += 1;
}
return false;
} | Method | Idea | Notes |
|---|---|---|
| i*i loop | Try candidates until square exceeds n | Clearest for beginners |
| Math.sqrt | root = Math.floor(Math.sqrt(n)); root * root === n | Fast and exact for integers |
| float sqrt | Math.round(Math.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 |
| Math.sqrt check | root = Math.floor(Math.sqrt(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 JavaScript programs — loop check for 16, list squares from 1 to 50 with Math.sqrt, 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.
function isPerfectSquare(number) {
if (number < 0) {
return false;
}
let i = 0;
while (i * i <= number) {
if (i * i === number) {
return true;
}
i += 1;
}
return false;
}
const testNumber = 16;
if (isPerfectSquare(testNumber)) {
console.log(`${testNumber} is a perfect square.`);
} else {
console.log(`${testNumber} 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 Math.floor(Math.sqrt(n)) for a crisp, exact check.
Use Math.floor(Math.sqrt(n)) and log all perfect squares from 1 to 50.
function isPerfectSquare(num) {
if (num < 0) {
return false;
}
const root = Math.floor(Math.sqrt(num));
return root * root === num;
}
console.log("Perfect Squares in the Range 1 to 50:");
let line = "";
for (let i = 1; i <= 50; i++) {
if (isPerfectSquare(i)) {
line += i + " ";
}
}
console.log(line.trim()); Math.floor(Math.sqrt(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.
console.log("First squares from k = 0 to 7:");
for (let k = 0; k <= 7; k++) {
const square = k * k;
console.log(`${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 Math.sqrt(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 Math.sqrt.
Loop for clarity; Math.sqrt for speed.
k*k builds the sequence without scanning.
Pro Tip: lead with the loop in interviews, then mention Math.floor(Math.sqrt(n)) 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.floor(Math.sqrt(n)) 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.floor(Math.sqrt(n)) over float sqrt.
Between 9 and 16 — not square.
4 * 4 = 16.
Handy follow-ups interviewers sometimes ask.
Math.floor(Math.sqrt(n)) ** 2 === n for n >= 0.Try these variations to lock in the pattern.
Math.floor(Math.sqrt(n)).Math.floor(Math.sqrt(n)) 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) |
| Math.sqrt-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.floor(Math.sqrt(n)), 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.
root * root === nDecide 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 JavaScript.
9 people found this page helpful