- Square
76² = 5776- Last 2 digits
- 76
- Verdict
- Suffix matches
n— automorphic.
Check Automorphic Number in JavaScript
What you’ll learn
- The automorphic condition in base 10:
n²ends with the same decimal representation asn. - Two equivalent JavaScript patterns:
n² mod 10k(withBigInt) and digit-by-digit comparison from the right. - Why
BigInthelps for the square, and why an integer loop for10kbeatsMath.pow(10, k)for exact modulus. - 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. This is not the same idea as “circular” numbers from other puzzles—those are a different family of digit patterns.
Two scripts
A single-value check (try 76) and a range scan (here 1 to 50).
Live preview
See n², the last k decimal digits, and the verdict instantly.
Interview polish
Exact modulus, overflow-safe squares, and clean guards for n ≤ 0.
Prerequisites
You should be comfortable with integer division, the modulo operator %, and using BigInt for exact large squares.
- JavaScript basics:
function,BigInt,console.log, andMath.floorfor digit loops. - The idea that
n % mreads the remainder after division bym(here powers of ten). - Optional contrast with Armstrong numbers (digit powers), which is a different test.
What is an automorphic number?
Let k be the number of decimal digits of a positive integer n. Then n is automorphic (in base 10) when the last k decimal digits of n² equal n. Equivalently,
n² mod 10k = n.
Single-digit examples are immediate: 5² = 25 ends in 5; 6² = 36 ends in 6. Two-digit examples include 25 and 76.
Mathematical definition
Work in base B = 10. Write 10k for the modulus that keeps the lowest k decimal digits. If n has exactly k ≥ 1 decimal digits, then n is automorphic when n² ≡ n (mod 10k).
n = 76Here k = 2 and 10k = 100. Now 76² = 5776, and 5776 mod 100 = 76, so 76 is automorphic.
Trimorphic numbers are the cube analogue: n³ ends in n in base 10. Automorphic always refers to the square here.
Intuition and examples
Write n² in decimal. Chop everything except the rightmost k digits, where k is how long n is. If what remains still reads as n, you have an automorphic number.
Each card shows n, n², and the last k digits of the square.
- Square
12² = 144- Last 2 digits
- 44
- Verdict
- 44 ≠ 12 — not automorphic.
- Square
25² = 625- Last 2 digits
- 25
- Verdict
- Classic two-digit example.
Takeaway: the modulus is always tied to the length of n in decimal, not to the value of n² in general.
Live preview
Enter a positive integer n. The widget uses JavaScript BigInt for n² so modestly large n stay exact. Values above 109 are blocked to keep the tab responsive.
- Try 76, 25, or 12.
- Press Run check (or Enter).
- Read
n², the extracted suffix, and the verdict.
Algorithm
Goal: decide whether a given positive integer n is automorphic in base 10.
Validate n
Reject n ≤ 0 if you only define automorphic numbers for positive integers.
Count decimal digits k
Repeatedly divide a temporary copy of n by 10 until it becomes 0.
Form n² and the modulus 10k
Store n² in a wide integer type. Build 10k with a loop, not floating pow.
Compare suffix to n
Return true if (n²) mod 10k == n. Alternatively, peel matching low digits from n and n² until n is exhausted.
List automorphic numbers in [low, high]
Loop each candidate i and print those that pass the test. Complexity grows with the width of the interval times the cost per test.
📜 Pseudocode
function digitCount(n):
k ← 0
t ← n
while t > 0:
k ← k + 1
t ← floor(t / 10)
return k
function pow10(k):
p ← 1
repeat k times:
p ← p * 10
return p
function isAutomorphic(n):
if n <= 0:
return false
k ← digitCount(n)
sq ← n * n // wide integer
return (sq mod pow10(k)) = nCheck a single number (suffix modulus)
Computes 10k with a short BigInt loop so the modulus stays exact. The square uses BigInt as well.
function pow10Big(k) {
let p = BigInt(1);
for (let i = 0; i < k; i++) {
p *= BigInt(10);
}
return p;
}
function digitCount(n) {
let k = 0;
let t = n;
while (t > 0) {
k++;
t = Math.floor(t / 10);
}
return k;
}
/** @param {number} num positive integer */
function isAutomorphic(num) {
if (num <= 0) {
return false;
}
const k = digitCount(num);
const nb = BigInt(num);
const square = nb * nb;
const mod = pow10Big(k);
return square % mod === nb;
}
const number = 76;
if (isAutomorphic(number)) {
console.log(number + " is an automorphic number.");
} else {
console.log(number + " is not an automorphic number.");
}Explanation
The last k decimal digits of a non-negative integer x are exactly x mod 10k when 10k fits your integer type.
if (num <= 0) return false;Positive definition here. Avoids a digit count of 0 and keeps the story aligned with interview prompts that say “given a natural number.”
const square = nb * nb;Widen before multiply. Squaring as a normal Number can lose precision; BigInt(num) keeps n² exact for the suffix test.
pow10Big(k)Exact modulus base. Replacing this with Math.pow(10, k) risks floating rounding once k grows.
return square % mod === nb;Suffix test. Compare the trimmed tail of n² to the original n as BigInt.
Automorphic numbers in a range (digit peel)
Same predicate as Example 1, implemented by stripping matching low digits from n and n². Uses BigInt for the square tail; no Math.pow required.
function isAutomorphicPeel(init) {
if (init <= 0) {
return false;
}
let num = init;
let square = BigInt(init) * BigInt(init);
while (num > 0) {
if (num % 10 !== Number(square % BigInt(10))) {
return false;
}
num = Math.floor(num / 10);
square = square / BigInt(10);
}
return true;
}
const start = 1;
const end = 50;
console.log("Automorphic numbers in the range " + start + " to " + end + ":");
let line = "";
for (let i = start; i <= end; i++) {
if (isAutomorphicPeel(i)) {
line += i + " ";
}
}
console.log(line.trim());Explanation
Each iteration checks one decimal place from the right. If every digit of n matches the corresponding tail digit of n², the number is automorphic.
if (num % 10 !== Number(square % BigInt(10))) return false;Local mismatch test. As soon as a digit disagrees, you can stop.
square = square / BigInt(10);Shift the square tail. Integer division drops the matched low digit for the next comparison.
Optimization
Precompute 10k. If you test many values with the same digit length, reuse one modulus instead of rebuilding it each time.
Batch filters. In a tight loop over [1, U], combine cheap rejects (parity, mod 10) only when you can prove correctness—do not guess wrong shortcuts.
Widen early. Use BigInt(n) before squaring whenever n is large enough that n * n as a Number would exceed Number.MAX_SAFE_INTEGER.
Interview: be ready to state both the modulus form and the digit-peel form; they are the same mathematics.
❓ FAQ
🔄 Input / output examples
For the single-number script, swap the literal number or read from process.argv / a prompt for interactive runs.
Value of n | 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. (with the sample guard) |
For the range script with 1 to 50:
Automorphic numbers in the range 1 to 50:
1 5 6 25Edge cases and pitfalls
Most failures are overflow on the square, a bad modulus from Math.pow, or mishandling n = 0 when counting digits.
Number square
Always use BigInt (or another exact path) before multiplying large n. Otherwise n² can round and the suffix test becomes meaningless.
Math.pow(10, k)
Use an integer loop to build 10k as BigInt. Floating rounding can break the modulus for larger k.
Digit count k = 0
A naive digit loop on 0 gives k = 0 and 100 = 1, which can wrongly mark 0 as automorphic in sloppy code. The samples return false for n ≤ 0.
10k overflow
For very large k, pow10Big grows beyond practical memory/time in naive loops. That only happens when n is enormous; switch strategies if your problem needs that range.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Single test (digit count + modulus or peel) | O(k) with k = O(log n) digits | O(1) |
All n in [1, U] | O(U log U) digit work (naive loop) | O(1) |
Both sample scripts use only a few scalars beyond the call stack.
Summary
- Definition:
n²ends withnin decimal ↔n² mod 10k = nforkdigits. - Code: wide square, exact
10k, or digit-by-digit peel from the right. - Watch-outs: do not confuse with circular numbers; avoid
Numberprecision loss on the square andMath.powrounding on the modulus.
In base 10, 25 is automorphic because 25² = 625 ends in 25. So is 76 (76² = 5776). These are not the same thing as “circular” or cyclic numbers—those refer to different digit-rotation ideas.
9 people found this page helpful
