Python Automorphic Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is an Automorphic Number?

An automorphic number is a positive integer n whose square ends with the same digits as n.

Remember
n is automorphic  ↔  n² ends with n

Example: 76 (k = 2 digits)
  76² = 5776  → last 2 digits = 76

Common: 1, 5, 6, 25, 76
nn²Ends with n?
25625Yes
765776Yes
525Yes
12144No

How to Check an Automorphic Number in Python

Count digits, then compare the last k digits of the square with n.

  1. Reject n <= 0 for this page’s positive-integer rule.
  2. Count digits into k with integer division //.
  3. Compute square = n * n.
  4. Check square % (10 ** k) == n.
MethodIdeaTimeBest for
Modulus suffixn² % 10^k == nO(log n)Interviews, default
String ends-withstr(n²).endswith(str(n))O(log n)Readable variant
Range scanReuse the helperO(N log N)Listing matches

Pseudocode

Pseudocode
function IsAutomorphic(n):
    if n <= 0:
        return false

    k = digitCount(n)
    square = n * n
    return (square mod 10^k) == n

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Testn² % 10^k == n, k = digits
Powers of 1010 ** k isolates the last k digits
1..501, 5, 6, 25
Classic25 → 625, 76 → 5776
Not12² = 144 (ends in 44)

Live Preview

Enter a positive integer to see n², the suffix, and the automorphic verdict.

Positive whole numbers. Tap a chip or type a value.

Result
n = 76, k = 2
76² = 5776
Last 2 digits = 76
Verdict: AUTOMORPHIC

Worked Walkthrough — n = 76

Square the number, then compare the last k digits with n.

Calculation
76 has 2 digits  →  k = 2

76² = 5776
5776 % 100 = 76

76 == 76  → Automorphic

Python Programs

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

How It Works

  1. Return False for non-positive inputs.
  2. Count digits into k by dividing a copy with //= 10.
  3. Square with n * n (Python ints grow as needed).
  4. Isolate the last k digits with square % (10 ** k).
  5. 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())

How It Works

  1. Loop from start to end inclusive with range(start, end + 1).
  2. Call is_automorphic(value) for each value.
  3. Print only the matches.
  4. Within 1…50 you should see 1, 5, 6, and 25.
  5. 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.

Python
def is_automorphic(n):
    if n <= 0:
        return False

    square = str(n * n)
    return square.endswith(str(n))


print(is_automorphic(25))  # True
print(is_automorphic(12))  # False

How It Works

  1. Convert n² and n to strings.
  2. endswith checks whether the square text finishes with the digits of n.
  3. 25² = 625 ends with “25” → True.
  4. 12² = 144 ends with “44” → False.
  5. Readable, but interviews usually prefer the modulus approach.

Edge Cases & Pitfalls

Check these before calling the solution done.

Digits

Use digit count k

Compare the last k digits — not a fixed two-digit modulus unless n is always two digits.

Zero

Definitions vary

This page returns False for n <= 0. Follow the problem statement if it includes 0.

Division

Use //

When counting digits, use t //= 10 — plain / yields a float in Python 3.

Ints

Python grows ints

Unlike fixed-width languages, Python integers expand, so large squares stay exact.

Time and Space Complexity

ProgramTimeExtra space
Single checkO(log n)O(1)
Range of size NO(N log N)O(1)
String suffixO(log n)O(log n) for strings

Digit count k is about log₁₀ n. Building 10 ** k costs O(k) work in practice for interview sizes.

Key Takeaways

  • Definition: n² ends with the same digits as n.
  • Classic: 25² = 625, 76² = 5776.
  • Check: n² % 10^k == n (or endswith).
  • Test set: 1, 5, 6, 25, 76 yes; 12 no.

One line: if the last digit-count digits of n² equal n, it is automorphic.

Frequently Asked Questions

A positive integer n is automorphic if n squared ends with n. Example: 25² = 625, which ends with 25.
Modulus gives the last k digits directly: n² % 10^k. Compare that with n — that is the whole check.
Some books include 0. This page treats n <= 0 as not automorphic and focuses on positive integers.
Yes. Convert n² to text and check if it ends with the digits of n using str.endswith. Interviews usually prefer the modulus method.
If k is the digit count, one check is O(k), and k is O(log n).

Next: Average of N Numbers

Find the mean of a list by summing and dividing by the count.

Average of N numbers tutorial →

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.

8 people found this page helpful