An automorphic number is a positive integer n whose square ends with the same digits as n.
Remember
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 JavaScript
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
String(n²).endsWith(String(n))
O(log n)
Readable variant
Range scan
Reuse the helper
O(N log N)
Listing matches
Pseudocode
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
Large n
Stay within safe integers, or use BigInt
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.
Positive whole numbers. Tap a chip or type a value.
Result
n = 76, k = 2
76² = 5776
Last 2 digits = 76
Verdict: AUTOMORPHIC
Code
JavaScript Programs
Start with a modulus suffix check. Then list a range, and finish with a readable string ends-with variant. Each example has a Try it Yourself editor.
Example 1 — Check One Number
Count digits, take (n * n) % pow10(k), and compare with n.
JavaScript
function pow10(k) {
let p = 1;
for (let i = 0; i < k; i++) {
p *= 10;
}
return p;
}
function isAutomorphic(n) {
if (n <= 0) {
return false;
}
let k = 0;
for (let t = n; t > 0; t = Math.floor(t / 10)) {
k++;
}
const square = n * n;
return square % pow10(k) === n;
}
const number = 76;
if (isAutomorphic(number)) {
console.log(number + " is an automorphic number.");
} else {
console.log(number + " is not an automorphic number.");
}
Square with n * n (safe for typical interview sizes).
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.
JavaScript
function pow10(k) {
let p = 1;
for (let i = 0; i < k; i++) {
p *= 10;
}
return p;
}
function isAutomorphic(n) {
if (n <= 0) {
return false;
}
let k = 0;
for (let t = n; t > 0; t = Math.floor(t / 10)) {
k++;
}
return (n * n) % pow10(k) === n;
}
const start = 1;
const end = 50;
console.log("Automorphic numbers in the range " + start + " to " + end + ":");
let line = "";
for (let value = start; value <= end; value++) {
if (isAutomorphic(value)) {
line += value + " ";
}
}
console.log(line.trim());