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 (int)sqrt approaches, a live checker, worked PHP 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 (int)sqrt 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 (int)sqrt.
Both are perfect squares.
Cast sqrt carefully for large n.
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 |
| (int)sqrt | $root = (int)sqrt($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 | for ($i = 1; $i * $i <= $n; $i++) |
| Exact hit | if ($i * $i === $n) return true; |
| sqrt cast check | $root = (int)sqrt($n); |
| Verify root | return $root * $root === $n; |
| Build squares | $k * $k for $k = 0, 1, 2, … |
Same question — different reliability.
$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 PHP programs — loop check for 16, list squares from 1 to 50 with (int)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.
<?php
function isPerfectSquare(int $number): bool
{
if ($number < 0) {
return false;
}
for ($i = 1; $i * $i <= $number; $i++) {
if ($i * $i === $number) {
return true;
}
}
return $number === 0;
}
$testNumber = 16;
echo isPerfectSquare($testNumber)
? $testNumber . " is a perfect square.\n"
: $testNumber . " is not a perfect square.\n";
?> Candidates advance while $i * $i has not passed 16. When $i reaches 4, the product matches and the function returns true. Zero is handled as a special case (0 = 0 * 0).
Use sqrt() with an integer cast for a short check.
Use (int)sqrt() and print all perfect squares from 1 to 50.
<?php
function isPerfectSquare(int $num): bool
{
if ($num < 0) {
return false;
}
$root = (int)sqrt($num);
return $root * $root === $num;
}
echo "Perfect Squares in the Range 1 to 50:\n";
for ($i = 1; $i <= 50; $i++) {
if (isPerfectSquare($i)) {
echo $i . " ";
}
}
echo "\n";
?> (int)sqrt($num) truncates the floating square root toward zero. Squaring that root recovers $num exactly when $num is a perfect square.
Build squares directly instead of filtering every integer.
<?php
echo "First squares from k = 0 to 7:\n";
for ($k = 0; $k <= 7; $k++) {
$square = $k * $k;
echo $k . " * " . $k . " = " . $square . "\n";
}
?> 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 (int)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 sqrt cast.
Loop for clarity; sqrt cast for speed.
k*k builds the sequence without scanning.
Pro Tip: lead with the loop in interviews, then mention (int)sqrt 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 (int)sqrt 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 (int)sqrt 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.
(int)sqrt.sqrt to int and square back, or use the $i * $i loop for huge n. The loop 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) |
| sqrt cast-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 (int)sqrt, 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 is also a quadratic residue in everyday arithmetic: the count of objects you can arrange in a square grid with the same number of rows and columns. The gaps between consecutive squares 1, 4, 9, 16… grow by the odd numbers 3, 5, 7, 9…
Learn how to find the average of N numbers in PHP.
9 people found this page helpful