PHP Perfect Square Program
Definition
What Is a Perfect Square?
A non-negative integer is a perfect square when it equals some integer k multiplied by itself: n = k × k.
n is a perfect square ⇔ ∃ integer k ≥ 0 such that k² = n
Examples:
0 = 0²
1 = 1²
9 = 3²
16 = 4²
10 → no integer square root
| n | √n idea | Perfect square? |
|---|
16 | 4 × 4 | Yes |
25 | 5 × 5 | Yes |
10 | between 3² and 4² | No |
0 | 0 × 0 | Yes |
Approach
How to Check a Perfect Square in PHP
Four clear steps — then pick an integer loop or a sqrt-and-verify check.
- Reject negatives (this tutorial treats only
n >= 0). - Try candidate roots
i = 0, 1, 2, … while i * i <= n. - If some
i * i == n, return true; if you pass n, return false. - Or compute
$root = (int) sqrt($n) and verify $root * $root == $n.
| Method | Idea | Time | Best for |
|---|
| Integer loop | Compare i * i with n | O(√n) | Interviews without floating point |
| sqrt + verify | Square the truncated root back | O(1) | Short production-style checks |
Pseudocode
function IsPerfectSquare(n):
if n < 0: return false
i = 0
while i * i <= n:
if i * i == n:
return true
i = i + 1
return false
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Definition | n = k² for some integer k ≥ 0 |
| Zero | 0 is a perfect square |
| Negatives | Not perfect squares here |
| Loop stop | While i * i <= n |
| sqrt verify | $root * $root == $n after truncate |
| Classic checks | 16 yes; 10 no; 25 yes |
Try it
Live Preview
Enter an integer to see the nearest integer root and the perfect-square check.
Trace
Worked Walkthrough — n = 16
Try successive integer squares until you match or pass 16.
i = 0 → 0² = 0
i = 1 → 1² = 1
i = 2 → 2² = 4
i = 3 → 3² = 9
i = 4 → 4² = 16 ← equal to n
16 is a perfect square
sqrt path:
sqrt(16) = 4.0
root = 4
4 * 4 == 16 → same answer
Code
PHP Programs
Start with the integer loop so you never rely on floating point. Use sqrt-and-verify when you want a short, clear check.
Example 1 — Integer Loop
Increase $i while $i * $i <= $n and look for an exact match.
<?php
function isPerfectSquare(int $n): bool
{
if ($n < 0) {
return false;
}
for ($i = 0; $i * $i <= $n; $i++) {
if ($i * $i === $n) {
return true;
}
}
return false;
}
$number = 16;
if (isPerfectSquare($number)) {
echo "$number is a perfect square\n";
} else {
echo "$number is not a perfect square\n";
}
How It Works
- Negatives return
false immediately. - The loop condition
$i * $i <= $n stops once squares go past $n. - Each step checks whether
$i * $i equals $n. - For
16, $i = 4 matches and the function returns true. - If no match appears before the loop ends, the answer is
false.
Example 2 — sqrt + Verify
Take the square root, truncate to an integer, then square it back.
<?php
function isPerfectSquare(int $n): bool
{
if ($n < 0) {
return false;
}
$root = (int) sqrt($n);
return $root * $root === $n;
}
echo isPerfectSquare(16) ? "true\n" : "false\n"; // true
echo isPerfectSquare(10) ? "true\n" : "false\n"; // false
echo isPerfectSquare(0) ? "true\n" : "false\n"; // true
echo isPerfectSquare(-4) ? "true\n" : "false\n"; // false
How It Works
- Same non-negative rule as Example 1.
sqrt($n) returns a float approximation of √n.- Casting to
int keeps the integer part of that root. - Squaring
$root again and comparing with $n confirms an exact square. - For
10, root becomes 3 and 9 != 10, so the result is false.
Edge Cases & Pitfalls
Check these before calling the solution done.
0Zero counts
0 = 0². Do not special-case it as false.
NegativesNot squares here
Return false for $n < 0 in this beginner definition.
Overflowi * i care
For very large $n, watch that $i * $i stays within PHP integer range.
sqrtAlways verify
Never decide from the float alone — square the truncated root back.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Integer loop | O(√n) | O(1) |
| sqrt + verify | O(1) | O(1) |
Both are fine for interviews. Prefer the loop when you must avoid floating-point discussion.
Remember
Key Takeaways
Definition: n = k² for some integer k ≥ 0.
Loop: test $i * $i while $i * $i <= $n.
sqrt: truncate, then verify $root * $root == $n.
Test set: 0 → yes; 16 → yes; 10 → no; -4 → no.
One line: find an integer root whose square equals n — by looping or by verifying sqrt.
Frequently Asked Questions
A non-negative integer that equals some integer multiplied by itself. Examples: 0, 1, 4, 9, 16, 25.
Loop with i*i until you match or pass n, or take (int)sqrt(n) and verify root*root == n.
Yes. 0 = 0 × 0.
sqrt returns a float. Floating-point rounding can misclassify edge values, so always square the integer root back and compare.
A loop up to √n is O(√n). The sqrt-and-verify method is O(1) for practical interview sizes (constant-time math call plus a few ops).
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
6 people found this page helpful