PHP Automorphic Number Program
Definition
What Is an Automorphic Number?
An automorphic number is a positive integer n whose square ends with the same digits as n.
n is automorphic ↔ n² ends with n
Example: 76 (k = 2 digits)
76² = 5776 → last 2 digits = 76
Common: 1, 5, 6, 25, 76
| n | n² | Ends with n? |
|---|
25 | 625 | Yes |
76 | 5776 | Yes |
5 | 25 | Yes |
12 | 144 | No |
Approach
How to Check an Automorphic Number in PHP
Count digits, then compare the last k digits of the square with n.
- Reject
$n <= 0 for this page’s positive-integer rule. - Count digits into
$k. - Compute
$square = $n * $n. - Check
$square % 10^$k == $n.
| Method | Idea | Time | Best for |
|---|
| Modulus suffix | n² % 10^k == n | O(log n) | Interviews, default |
| String ends-with | Square text ends with n text | O(log n) | Readable variant |
| Range scan | Reuse the helper | O(N log N) | Listing matches |
Pseudocode
function IsAutomorphic(n):
if n <= 0:
return false
k = digitCount(n)
square = n * n
return (square mod 10^k) == n
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Test | n² % 10^k == n, k = digits |
| Modulus | Build 10^k with a small loop |
| 1..50 | 1, 5, 6, 25 |
| Classic | 25 → 625, 76 → 5776 |
| Not | 12² = 144 (ends in 44) |
Try it
Live Preview
Enter a positive integer to see n², the suffix, and the automorphic verdict.
Trace
Worked Walkthrough — n = 76
Square the number, then compare the last k digits with n.
76 has 2 digits → k = 2
76² = 5776
5776 % 100 = 76
76 == 76 → Automorphic
Code
PHP Programs
Start with a modulus suffix check. Then list a range, and finish with a readable string ends-with variant.
Example 1 — Check One Number
Count digits, take ($n * $n) % pow10($k), and compare with $n.
<?php
function pow10(int $k): int {
$p = 1;
for ($i = 0; $i < $k; $i++) {
$p *= 10;
}
return $p;
}
function isAutomorphic(int $n): bool {
if ($n <= 0) {
return false;
}
$k = 0;
for ($t = $n; $t > 0; $t = intdiv($t, 10)) {
$k++;
}
$square = $n * $n;
return $square % pow10($k) === $n;
}
$number = 76;
if (isAutomorphic($number)) {
echo $number . " is an automorphic number.\n";
} else {
echo $number . " is not an automorphic number.\n";
}
?>
76 is an automorphic number.
How It Works
- Return
false for non-positive inputs. - Count digits into
$k by dividing a copy by 10. - Square with
$n * $n. - Isolate the last k digits with
$square % pow10($k). - Equality with
$n is the whole definition.
Example 2 — Automorphic Numbers from 1 to 50
Reuse the helper and print every match in a closed range.
<?php
function pow10(int $k): int {
$p = 1;
for ($i = 0; $i < $k; $i++) {
$p *= 10;
}
return $p;
}
function isAutomorphic(int $n): bool {
if ($n <= 0) {
return false;
}
$k = 0;
for ($t = $n; $t > 0; $t = intdiv($t, 10)) {
$k++;
}
$square = $n * $n;
return $square % pow10($k) === $n;
}
$start = 1;
$end = 50;
echo "Automorphic numbers in the range " . $start . " to " . $end . ":\n";
for ($value = $start; $value <= $end; $value++) {
if (isAutomorphic($value)) {
echo $value . " ";
}
}
echo "\n";
?>
Automorphic numbers in the range 1 to 50:
1 5 6 25
How It Works
- Loop from
$start to $end inclusive. - Call
isAutomorphic($value) for each value. - Print only the matches.
- Within 1…50 you should see 1, 5, 6, and 25.
- The helper stays pure; the loop only decides what to print.
Example 3 — String Suffix Check
Convert the square to text and ask whether it ends with the digits of n.
<?php
function isAutomorphic(int $n): bool {
if ($n <= 0) {
return false;
}
$square = (string) ($n * $n);
$ns = (string) $n;
return substr($square, -strlen($ns)) === $ns;
}
echo isAutomorphic(25) ? "true\n" : "false\n";
echo isAutomorphic(12) ? "true\n" : "false\n";
?>
How It Works
- Format
$n² and $n as strings. - Use
substr(..., -strlen($ns)) to take the square’s suffix. - 25² = 625 ends with “25” →
true. - 12² = 144 ends with “44” →
false. - Readable, but interviews usually prefer the modulus approach.
Edge Cases & Pitfalls
Check these before calling the solution done.
OverflowKeep n modest
Very large $n can make $n * $n exceed int range on 32-bit PHP. Use interview-sized values.
DigitsUse digit count k
Compare the last k digits — not a fixed two-digit modulus unless n is always two digits.
ZeroDefinitions vary
This page returns false for $n <= 0. Follow the problem statement if it includes 0.
Not circularDifferent from circular primes
Automorphic checks the suffix of n². Circular or cyclic ideas are unrelated.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Single check | O(log n) | O(1) |
| Range of size N | O(N log N) | O(1) |
| String suffix | O(log n) | O(log n) for strings |
Digit count k is about log₁₀ n. Building 10^k costs O(k) multiplies.
Remember
Key Takeaways
Definition: n² ends with the same digits as n.
Classic: 25² = 625, 76² = 5776.
Check: n² % 10^k == n.
Test set: 1, 5, 6, 25, 76 yes; 12 no.
One line: if the last digit-count digits of n² equal n, it is automorphic.
Frequently Asked Questions
A positive integer n is automorphic if n squared ends with n. Example: 25² = 625, which ends with 25.
Modulus gives the last k digits directly: n² % 10^k. Compare that with n — that is the whole check.
Some books include 0. This page treats n <= 0 as not automorphic and focuses on positive integers.
Yes. Convert n² to text and check if it ends with the digits of n. Interviews usually prefer the modulus method.
If k is the digit count, one check is O(k), and k is O(log n).
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.
18 people found this page helpful