- Square
- 625
- Last 2 digits
- 25
Check Automorphic Number in Python
What you’ll learn
- What automorphic means with easy examples.
- How to check suffix using
n*n % 10^k. - Two Python programs: single check and range listing.
- Edge cases and interview-friendly complexity notes.
Overview
A number is automorphic if its square ends with the same digits as the number itself. Example: 76² = 5776 ends with 76.
What is an automorphic number?
If n has k digits, then check whether n*n % 10^k == n. If yes, n is automorphic.
This page treats positive integers only, so n <= 0 is not considered automorphic.
Intuition and examples
- Square
- 5776
- Last 2 digits
- 76
- Square
- 144
- Last 2 digits
- 44
Live preview
Enter n and see n², suffix, and verdict.
Algorithm
Goal: decide if n is automorphic.
Validate n
If n <= 0, return false.
Find digit count
Compute k = len(str(n)).
Compute suffix
Set suffix = n*n % (10**k).
Compare
If suffix equals n, true; else false.
📜 Pseudocode
function isAutomorphic(n):
if n <= 0:
return false
k = number of digits of n
return (n*n mod 10^k) == nCheck a single number (modulus method)
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.")Automorphic numbers in a range
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=" ")❓ FAQ
Edge cases and pitfalls
Return false
This tutorial uses positive integers only.
Big square values
Python handles big ints, but browser/live preview is capped for responsiveness.
Not circular number
Automorphic and circular/cyclic are different concepts.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Single check | O(log n) | O(1) extra |
| Range 1..U | O(U log U) | O(1) extra |
Summary
- Rule: n² ends with n.
- Code: use suffix modulus
(n*n) % (10**k). - Practice: try 5, 6, 25, 76 and nearby non-examples.
In base 10, 5, 6, 25, and 76 are common automorphic numbers because their squares end with the same digits.
9 people found this page helpful
