- Square
76² = 5776- Last 2 digits
- 76
- Verdict
- Suffix matches
n.
Check Automorphic Number in PHP
What you’ll learn
- The automorphic condition in base 10:
n²ends with the same decimal representation asn. - Two equivalent PHP patterns:
n² mod 10kand digit-by-digit comparison from the right. - Why integer
10kbeats floatingpowfor exact suffix checks. - A browser live preview, edge cases, and complexity notes for interviews.
Overview
An automorphic number (in base 10) is a positive integer n whose square ends in n. Example: 25² = 625 ends in 25.
Two programs
A single-value check and a range scan.
Live preview
See n², the suffix, and the verdict instantly.
Interview polish
Exact integer modulus and clean guards for n ≤ 0.
Prerequisites
You should be comfortable with loops, %, and intdiv().
- PHP basics: variables,
if,for/while, functions. - Digit operations with
$n % 10andintdiv($n, 10). - Optional contrast with Armstrong numbers.
What is an automorphic number?
Let k be the number of decimal digits of a positive integer n. Then n is automorphic when the last k decimal digits of n² equal n.
n² mod 10k = n.
Mathematical definition
Work in base 10. If n has exactly k ≥ 1 decimal digits, then n is automorphic when n² ≡ n (mod 10k).
n = 7676² = 5776, and 5776 mod 100 = 76, so 76 is automorphic.
Intuition and examples
- Square
12² = 144- Last 2 digits
- 44
- Verdict
- 44 ≠ 12.
- Square
25² = 625- Last 2 digits
- 25
- Verdict
- Classic two-digit example.
Live preview
Enter a positive integer n. The widget uses JavaScript BigInt for exact n² and suffix checks.
Algorithm
Goal: decide whether a positive integer n is automorphic in base 10.
Validate n
Reject n ≤ 0.
Count decimal digits k
Repeatedly divide by 10 using intdiv.
Form square and modulus
Compute n², then build 10k with a loop.
Compare suffix
Return true if (n²) mod 10k == n.
📜 Pseudocode
function digitCount(n):
k <- 0
t <- n
while t > 0:
k <- k + 1
t <- floor(t / 10)
return k
function isAutomorphic(n):
if n <= 0:
return false
k <- digitCount(n)
return (n*n mod 10^k) = nCheck a single number (suffix modulus)
Computes 10k using integer multiplication so suffix extraction stays exact.
<?php
function pow10int(int $k): int
{
$p = 1;
for ($i = 0; $i < $k; $i++) {
$p *= 10;
}
return $p;
}
function isAutomorphic(int $num): bool
{
if ($num <= 0) {
return false;
}
$k = 0;
$t = $num;
while ($t > 0) {
$k++;
$t = intdiv($t, 10);
}
$square = $num * $num;
$mod = pow10int($k);
return ($square % $mod) === $num;
}
$number = 76;
echo isAutomorphic($number)
? $number . " is an automorphic number."
: $number . " is not an automorphic number.";
?>Automorphic numbers in a range (digit peel)
Same predicate as Example 1, implemented by comparing low digits of n and n².
<?php
function isAutomorphicPeel(int $number): bool
{
if ($number <= 0) {
return false;
}
$square = $number * $number;
while ($number > 0) {
if (($number % 10) !== ($square % 10)) {
return false;
}
$number = intdiv($number, 10);
$square = intdiv($square, 10);
}
return true;
}
$start = 1;
$end = 50;
echo "Automorphic numbers in the range $start to $end:\\n";
for ($i = $start; $i <= $end; $i++) {
if (isAutomorphicPeel($i)) {
echo $i . " ";
}
}
?>Optimization
Precompute 10k. If many values share a digit length, reuse one modulus.
Use integer arithmetic. Keep suffix checks exact.
Choose one explanation style. Modulus and digit-peel are equivalent mathematically.
❓ FAQ
🔄 Input / output examples
Value of $number | Typical line printed |
|---|---|
| 76 | 76 is an automorphic number. |
| 12 | 12 is not an automorphic number. |
| 5 | 5 is an automorphic number. |
| 0 | 0 is not an automorphic number. |
Edge cases and pitfalls
n ≤ 0
Return false early for beginner-friendly behavior.
pow(10, k)
Prefer integer multiplication for exact suffix checks.
Large square
Very large n can exceed integer limits.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Single test | O(log n) | O(1) |
All n in [1, U] | O(U log U) | O(1) |
Summary
- Definition:
n²ends withn. - Code: use modulus or digit-peel approach with integers.
- Watch-outs: input guards, floating behavior, and very large squares.
In base 10, 25 is automorphic because 25² = 625 ends in 25. So is 76 (76² = 5776).
9 people found this page helpful
