Check Automorphic Number in Python

Beginner
⏱️ 11 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digits & modulus

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

25Automorphic
Square
625
Last 2 digits
25
76Automorphic
Square
5776
Last 2 digits
76
12Not automorphic
Square
144
Last 2 digits
44

Live preview

Enter n and see n², suffix, and verdict.

Use integers n ≥ 1.

Live result
Press "Run check" to see details.

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

Pseudocode
function isAutomorphic(n):
    if n <= 0:
        return false
    k = number of digits of n
    return (n*n mod 10^k) == n
1

Check a single number (modulus method)

python
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.")
2

Automorphic numbers in a range

python
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

A positive integer n is automorphic if n squared ends with n. Example: 25^2 = 625, which ends with 25.
No. Automorphic checks the suffix of n^2. Circular/cyclic number concepts are different.
Some books include 0. In this tutorial, we focus on positive integers, so n <= 0 returns false.
Modulus gives the last k digits directly: n^2 % 10^k. That is exactly what we need to compare with n.
If k is number of digits, one check is O(k), and k is O(log n).
Loop through the range and call the same checker function for each number.

Edge cases and pitfalls

n <= 0

Return false

This tutorial uses positive integers only.

Large n

Big square values

Python handles big ints, but browser/live preview is capped for responsiveness.

Terminology

Not circular number

Automorphic and circular/cyclic are different concepts.

⏱️ Time and space complexity

TaskTimeExtra space
Single checkO(log n)O(1) extra
Range 1..UO(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.
Did you know?

In base 10, 5, 6, 25, and 76 are common automorphic numbers because their squares end with the same digits.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful