Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Check Automorphic Number
Photo Credit to CodeToFun
π Introduction
In the realm of python programming, exploring unique number properties is both fascinating and educational. An automorphic number is one such interesting concept.
An automorphic number (or circular number) is a number whose square ends with the number itself. In simpler terms, an n-digit number is called an automorphic number if the last n digits of its square are equal to the number itself.
In this tutorial, we'll delve into a Python program that checks whether a given number is an automorphic number or not.
The program will take a number as input, square it, and then compare the last digits to determine if it meets the criteria of an automorphic number.
π Example
Let's explore the Python code that accomplishes this task.
def is_automorphic(num):
# Calculate the square of the number
square = num ** 2
# Count the number of digits in the original number
original_digits = len(str(num))
# Extract the last digits from the square
last_digits = square % 10**original_digits
# Check if the last digits match the original number
return last_digits == num
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
number = 76
# Check if the number is automorphic
if is_automorphic(number):
print(f"{number} is an automorphic number.")
else:
print(f"{number} is not an automorphic number.")
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the if __name__ == "__main__": block.
76 is an automorphic number.
Run the script to check if the given number is an automorphic number.
π§ How the Program Works
- The program defines a function is_automorphic that takes a number as input and returns whether it is an automorphic number or not.
- Inside the function, it calculates the square of the input number.
- It counts the number of digits in the original number to extract the corresponding number of last digits from the square.
- It compares the extracted last digits with the original number to determine if it's an automorphic number.
π Between the Given Range
Let's explore the Python code that identifies Automorphic Numbers within the specified range.
# Function to check if a number is Automorphic
def is_automorphic(num):
square = num ** 2
str_num = str(num)
str_square = str(square)
return str_square.endswith(str_num)
# Check Automorphic Numbers in the range 1 to 50
automorphic_numbers = [num for num in range(1, 51) if is_automorphic(num)]
# Display the results
print("Automorphic Numbers in the Range 1 to 50:")
for num in automorphic_numbers:
print(num, end=" ")
π» Testing the Program
Automorphic Numbers in the Range 1 to 50: 1 5 6 25
Run the Python script, and it will display the Automorphic Numbers in the range of 1 to 50.
π§ How the Program Works
- The program defines a function is_automorphic that takes a number as input, calculates its square, and checks if the square ends with the same digits as the original number.
- It then uses a list comprehension to generate a list of Automorphic Numbers within the range of 1 to 50.
- Finally, the program prints the identified Automorphic Numbers.
π§ Understanding the Concept of Automorphic Numbers
Before delving into the code, let's take a moment to understand automorphic numbers.
An n-digit number is called an automorphic number if the last n digits of its square are equal to the number itself.
For example, 25 is an automorphic number because its square is 625, and the last two digits (25) match the original number.
π’ Optimizing the Program
While the provided program effectively checks for automorphic numbers, you can explore and implement further optimizations or variations based on your specific needs or preferences.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Automorphic Number), please comment here. I will help you immediately.