Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Check if a number with even number of digits is palindrome or not
A palindrome is a number that reads the same forwards and backwards. This article shows how to check if a number with an even number of digits is a palindrome in Python. Examples include 2662, 4224, 44, 1001, etc.
Using String Comparison
The simplest approach converts the number to a string and compares it with its reverse ?
def is_even_digit_palindrome(num):
# Convert number to string
num_str = str(num)
# Check if even number of digits
if len(num_str) % 2 != 0:
return False
# Check if palindrome
return num_str == num_str[::-1]
# Test with even digit numbers
numbers = [2662, 4224, 44, 1001, 123321, 12321]
for num in numbers:
result = is_even_digit_palindrome(num)
digit_count = len(str(num))
print(f"{num} ({digit_count} digits): {'Palindrome' if result else 'Not palindrome'}")
2662 (4 digits): Palindrome 4224 (4 digits): Palindrome 44 (2 digits): Palindrome 1001 (4 digits): Palindrome 123321 (6 digits): Palindrome 12321 (5 digits): Not palindrome
Using Mathematical Approach
This method reverses the number mathematically without string conversion ?
def reverse_number(num):
"""Reverse a number mathematically"""
reversed_num = 0
while num > 0:
reversed_num = reversed_num * 10 + num % 10
num //= 10
return reversed_num
def count_digits(num):
"""Count number of digits in a number"""
if num == 0:
return 1
count = 0
while num > 0:
count += 1
num //= 10
return count
def is_even_digit_palindrome_math(num):
# Count digits
digit_count = count_digits(num)
# Check if even number of digits
if digit_count % 2 != 0:
return False
# Check if palindrome
return num == reverse_number(num)
# Test the mathematical approach
test_numbers = [2662, 4224, 44, 1001, 8888]
for num in test_numbers:
result = is_even_digit_palindrome_math(num)
digits = count_digits(num)
print(f"{num} ({digits} digits): {'Even-digit palindrome' if result else 'Not even-digit palindrome'}")
2662 (4 digits): Even-digit palindrome 4224 (4 digits): Even-digit palindrome 44 (2 digits): Even-digit palindrome 1001 (4 digits): Even-digit palindrome 8888 (4 digits): Even-digit palindrome
Finding All Even-Digit Palindromes in a Range
This function generates all even-digit palindromes within a specified range ?
def find_even_digit_palindromes(start, end):
"""Find all even-digit palindromes in a range"""
palindromes = []
for num in range(start, end + 1):
num_str = str(num)
# Check if even number of digits and palindrome
if len(num_str) % 2 == 0 and num_str == num_str[::-1]:
palindromes.append(num)
return palindromes
# Find even-digit palindromes between 1 and 10000
palindromes = find_even_digit_palindromes(10, 10000)
print(f"Even-digit palindromes between 10 and 10000:")
print(f"Total found: {len(palindromes)}")
print(f"First 20: {palindromes[:20]}")
Even-digit palindromes between 10 and 10000: Total found: 108 First 20: [11, 22, 33, 44, 55, 66, 77, 88, 99, 1001, 1111, 1221, 1331, 1441, 1551, 1661, 1771, 1881, 1991, 2002]
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| String Comparison | O(n) | O(n) | Simple implementation |
| Mathematical | O(n) | O(1) | Memory efficiency |
| Range Generation | O(n×m) | O(k) | Finding multiple palindromes |
Conclusion
Use string comparison for simplicity and readability. The mathematical approach is more memory-efficient for large numbers. Both methods effectively check if a number with an even digit count is a palindrome.
Advertisements
