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) % (10 ** k), and compare with n.
Python
def is_automorphic(n):
if n <= 0:
return False
k = 0
t = n
while t > 0:
k += 1
t //= 10
square = n * n
return square % (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.")
Output
76 is an automorphic number.
How It Works
Return False for non-positive inputs.
Count digits into k by dividing a copy with //= 10.
Square with n * n (Python ints grow as needed).
Isolate the last k digits with square % (10 ** 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.
Python
def is_automorphic(n):
if n <= 0:
return False
k = 0
t = n
while t > 0:
k += 1
t //= 10
return (n * n) % (10 ** k) == n
start = 1
end = 50
print(f"Automorphic numbers in the range {start} to {end}:")
line = ""
for value in range(start, end + 1):
if is_automorphic(value):
line += str(value) + " "
print(line.strip())
Output
Automorphic numbers in the range 1 to 50:
1 5 6 25
How It Works
Loop from start to end inclusive with range(start, end + 1).
Call is_automorphic(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.