Definition
n² ends in n
n is automorphic when the last digits of n² are exactly n.
An automorphic number’s square ends with the number itself. This tutorial covers the definition, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
n² ends in n
n is automorphic when the last digits of n² are exactly n.
Find length
Use len(str(n)) so you know how many trailing digits to compare.
n² % 10^k
The last k digits of n² equal (n * n) % (10 ** k).
76² = 5776
5776 ends with 76 — a golden interview example alongside 25.
Try any n
Type a number and see n², the modulus suffix, and the verdict.
Complexity
One check depends on digit count; extra space stays O(1).
An automorphic number is a positive integer n whose square ends with the digits of n itself. If n has k digits, the check is simply (n * n) % (10 ** k) == n.
Famous base-10 examples include 5 (5² = 25), 6 (6² = 36), 25 (25² = 625), and 76 (76² = 5776). This tutorial treats only positive integers, so n <= 0 returns false.
It trains digit counting, powers of ten, and modulus suffix tricks — tools that show up in many interview number problems.
Only the last k digits of n² must equal n.
% 10^k peels those last k digits in one shot.
Automorphic is a square-suffix property — different from cyclic numbers.
Squares grow fast; Python integers stay exact.
In short: count digits k, then check whether (n * n) % (10 ** k) equals n.
Given a positive integer n, decide whether it is automorphic: whether n² ends with n.
# Example: n = 76 (k = 2)
# 76 * 76 = 5776
# 5776 % 100 == 76 → automorphic | Item | Type | Description |
|---|---|---|
n | int | Positive integer to test (this tutorial returns false for n <= 0). |
| Return / print | bool / text | True / message when the last k digits of n² equal n. |
function isAutomorphic(n):
if n <= 0:
return false
k = number of digits of n
return (n * n mod 10^k) == n | Method | Idea | Notes |
|---|---|---|
| Modulus suffix | (n * n) % (10 ** k) == n | Interview classic; O(1) extra space |
| String ends-with | str(n * n).endswith(str(n)) | Very readable; allocates strings |
| Goal | Pattern |
|---|---|
| Digit count k | k = len(str(n)) |
| Mask for last k digits | 10 ** k |
| Square suffix | (n * n) % (10 ** k) |
| Automorphic test | suffix == n |
| String variant | str(n * n).endswith(str(n)) |
| Common examples | 1, 5, 6, 25, 76 |
All look similar — only one idea is correct for automorphic.
% 10^kBest default for interviews; peels the suffix directly
str ends withClear and short; fine when readability wins
wrong ideaAutomorphic cares about the end of n², not the start
explain % 10^kShow you know why the modulus isolates last digits
Reach for automorphic drills when suffix checks and modulus matter.
Quick check of digit count, powers of ten, and boolean returns.
Natural follow-up once students already count digits.
Makes % 10^k feel concrete with 25 and 76.
“Print all automorphic numbers from 1 to N” reuses one helper.
Very large n make enormous squares — cap previews and discuss big integers.
Key benefit: one short problem that covers digit length, modulus suffixes, and O(log n) reasoning.
Enter a positive integer to see n², the last-k-digit suffix, and the verdict.
Three complete Python programs — modulus check, range listing, and a string endswith variant. Click View Output to reveal sample console results.
Modulus suffix — the interview default.
Count digits, take (n * n) % (10 ** k), and compare with n.
def is_automorphic(n: int) -> bool:
if n <= 0:
return False
k = len(str(n))
return (n * n) % (10 ** k) == n
number = 76
if is_automorphic(number):
print(f"{number} is an automorphic number.")
else:
print(f"{number} is not an automorphic number.") Guard non-positive inputs, compute k once, then isolate the last k digits of the square with modulus. Equality with n is the entire definition.
Reuse the helper across a closed range.
Loop from start to end and print every value that passes the check.
def is_automorphic(n: int) -> bool:
if n <= 0:
return False
k = len(str(n))
return (n * n) % (10 ** k) == n
start, end = 1, 50
print(f"Automorphic numbers in the range {start} to {end}:")
for value in range(start, end + 1):
if is_automorphic(value):
print(value, end=" ") The helper stays pure; the outer loop only decides what to print. Within 1…50 you should see 1, 5, 6, and 25 — a useful self-check.
Same verdict with string suffix matching.
endswith CheckConvert the square to text and ask whether it ends with the digits of n.
def is_automorphic(n: int) -> bool:
if n <= 0:
return False
return str(n * n).endswith(str(n))
print(is_automorphic(25))
print(is_automorphic(12)) endswith does the suffix comparison for you. 25² = 625 ends with “25”; 12² = 144 ends with “44”, so the second call is false.
If n <= 0, return false for this tutorial’s positive-integer definition.
Set k = len(str(n)) (or count by dividing by 10).
Compute (n * n) % (10 ** k) to read the last k digits of the square.
Return true only when that suffix equals the original number.
n = 76Trace the modulus method. Digit count k = 2, so the mask is 10 ** 2 = 100.
| Step | Expression | Result |
|---|---|---|
| 1 | k = len("76") | 2 |
| 2 | 76 * 76 | 5776 |
| 3 | 5776 % 100 | 76 |
| 4 | 76 == 76 | True (automorphic) |
Contrast: for n = 12, 144 % 100 = 44, which is not 12.
Where automorphic checks show up beyond the interview prompt.
Warm-up for digit length and modulus suffixes.
Example: write is_automorphic(n).
Shows how modulus isolates the last k digits.
Example: chalkboard walkthrough of 76.
Print or count automorphic values inside bounds.
Example: all hits from 1 to 100.
Skills transfer to other suffix / prefix number checks.
Example: trimorphic or Kaprekar follow-ups.
Argue O(log n) from digit count convincingly.
Example: “why is k = O(log n)?”
Squares grow quickly; Python still keeps exact values.
Example: discuss fixed-width overflow in other languages.
Pro Tip: keep one is_automorphic helper and reuse it for single checks and range printers.
Why this pattern works well in interviews and classwork.
After counting digits, the modulus comparison is a single clear expression.
Using k from the digit count handles 1-digit through multi-digit cases.
A few integers suffice for the modulus approach — O(1) extra space.
5, 6, 25, 76 and 12 give instant confidence.
Pro Tip: say “last k digits of n²” out loud before coding — it prevents accidental startswith mistakes.
Small habits that keep automorphic code interview-ready.
Compute k before squaring so the modulus mask matches the original length.
It shows number sense; mention endswith only as an alternative.
Return false early for n <= 0 unless the prompt includes 0.
Assert True on 5/25/76 and False on 12 before moving on.
If asked about circular numbers, say they are a different concept.
Pro Tip: dry-run 76 on paper once — it locks in why % 100 is the right mask for a 2-digit n.
Mistakes that commonly break automorphic solutions.
Asking whether n² starts with n is a different (wrong) problem.
→ Always compare the suffix / last k digits.
Using a fixed % 100 only works for 2-digit numbers.
→ Build the mask as 10 ** k from the digit count.
k must be the length of n, not the length of the square.
→ Call len(str(n)) before squaring for the mask.
Different definitions; mixing them fails interviews.
→ Stick to “n² ends with n.”
In fixed-width languages, n*n may overflow before the suffix check.
→ In Python you are safe; mention care in C/Java interviews.
Check these inputs before calling the solution done.
This tutorial uses positive integers only.
Common automorphic values — good smoke tests.
Must return true for any correct implementation.
144 ends in 44 — must return false.
Python ints stay exact; live preview is capped for speed.
Automorphic ≠ circular/cyclic number.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
is_automorphicstr for digit countendswith versions10 ** k.Quick Takeaway: if the last k digits of n² equal n, the number is automorphic.
| Program | Time | Extra space |
|---|---|---|
| Single modulus check | O(log n) | O(1) |
String endswith check | O(log n) | O(log n) for the strings |
| Range 1…U | O(U log U) | O(1) |
Automorphic numbers are a clean suffix exercise: count digits k, take (n * n) % (10 ** k), and compare with n. Master the modulus method first, then the string variant when you want shorter code.
Practice the three examples above, then continue to the average-of-N-numbers tutorial for a different classic interview warm-up.
Never check the prefix of n², never hard-code % 100 for every length, and always verify 25 and 76.
(n * n) % (10 ** k)n <= 0Check square suffixes the interview-friendly way.
n² ends with n
Definitionk = length of n
Mathn² % 10^k
Code76² = 5776
ExampleO(log n) time
AnalysisIn base 10, 5, 6, 25, and 76 are common automorphic numbers because their squares end with the same digits.
Learn how to compute the average of a list of numbers in Python.
9 people found this page helpful