0
0
PythonProgramBeginner · 2 min read

Python Program to Find Automorphic Number

An automorphic number is a number whose square ends with the number itself; in Python, you can check this by converting both the number and its square to strings and using str(square).endswith(str(number)).
📋

Examples

Input5
Output5 is an automorphic number
Input76
Output76 is an automorphic number
Input13
Output13 is not an automorphic number
🧠

How to Think About It

To find if a number is automorphic, first square the number. Then check if the last digits of the square match the original number. This can be done by comparing the string form of the number with the ending part of the string form of its square.
📐

Algorithm

1
Get the input number
2
Calculate the square of the number
3
Convert both the number and its square to strings
4
Check if the square string ends with the number string
5
If yes, return that the number is automorphic; otherwise, it is not
💻

Code

python
number = int(input("Enter a number: "))
square = number ** 2
if str(square).endswith(str(number)):
    print(f"{number} is an automorphic number")
else:
    print(f"{number} is not an automorphic number")
Output
Enter a number: 5 5 is an automorphic number
🔍

Dry Run

Let's trace the number 5 through the code

1

Input number

number = 5

2

Calculate square

square = 5 ** 2 = 25

3

Convert to strings

str(square) = '25', str(number) = '5'

4

Check if square ends with number

'25'.endswith('5') is True

5

Print result

Output: '5 is an automorphic number'

numbersquarestr(square)str(number)endswith check
525'25''5'True
💡

Why This Works

Step 1: Square the number

We find the square because automorphic numbers have their square ending with the number itself.

Step 2: Convert to strings

Converting to strings allows easy comparison of the ending digits using endswith.

Step 3: Check ending digits

Using endswith checks if the square's string ends exactly with the number's string.

🔄

Alternative Approaches

Using modulo operator
python
number = int(input("Enter a number: "))
length = len(str(number))
square = number ** 2
if square % (10 ** length) == number:
    print(f"{number} is an automorphic number")
else:
    print(f"{number} is not an automorphic number")
This method uses math instead of strings, which can be faster and avoids string conversion.
Using recursion to check digits
python
def is_automorphic(num):
    square = num ** 2
    def check(n, sq):
        if n == 0:
            return True
        if n % 10 != sq % 10:
            return False
        return check(n // 10, sq // 10)
    return check(num, square)

number = int(input("Enter a number: "))
if is_automorphic(number):
    print(f"{number} is an automorphic number")
else:
    print(f"{number} is not an automorphic number")
This recursive approach compares digits from right to left without converting to strings.

Complexity: O(d) time, O(1) space

Time Complexity

The time depends on the number of digits d in the input number because string conversion and endswith check operate on digit length.

Space Complexity

Only a few variables and string conversions are used, so space is constant O(1).

Which Approach is Fastest?

The modulo method is generally faster than string conversion because it uses arithmetic operations without creating new strings.

ApproachTimeSpaceBest For
String endswithO(d)O(1)Readability and simplicity
Modulo operatorO(d)O(1)Performance and no string use
Recursive digit checkO(d)O(d)Learning recursion and digit manipulation
💡
Use str(square).endswith(str(number)) for a simple and readable automorphic check.
⚠️
Beginners often forget to compare only the last digits of the square, checking the whole number instead.