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
Check if any permutation of a number is divisible by 3 and is Palindromic in Python
This task involves checking if any permutation of a given number's digits can form a new number that is both divisible by 3 and palindromic. A number is divisible by 3 if the sum of its digits is divisible by 3, and a number is palindromic if it reads the same forwards and backwards.
Understanding the Problem
For a number to be divisible by 3, the sum of its digits must be divisible by 3. For it to be palindromic, the number must read the same when reversed. We need to check if any permutation of the input number satisfies both conditions.
Scenario 1
<b>Input:</b> 121 <b>Output:</b> False <b>Explanation:</b> The permutations are: 121, 112, 211 Only 121 is palindromic, but sum of digits (1+2+1=4) is not divisible by 3.
Scenario 2
<b>Input:</b> 303 <b>Output:</b> True <b>Explanation:</b> The permutations include: 033, 303, 330 303 is palindromic and sum of digits (3+0+3=6) is divisible by 3.
Method 1: Using Permutations and Helper Functions
We can generate all permutations of the digits and check each one for both conditions ?
from itertools import permutations
def is_divisible_by_3(num_str):
return sum(int(digit) for digit in num_str) % 3 == 0
def is_palindromic(num_str):
return num_str == num_str[::-1]
def check_permutation_conditions(n):
digits = str(n)
seen_permutations = set()
for perm in permutations(digits):
# Skip numbers starting with 0 (invalid numbers)
if perm[0] == '0':
continue
perm_str = ''.join(perm)
# Skip duplicate permutations
if perm_str in seen_permutations:
continue
seen_permutations.add(perm_str)
# Check if both conditions are satisfied
if is_palindromic(perm_str) and is_divisible_by_3(perm_str):
return True
return False
# Test the function
print(check_permutation_conditions(121))
print(check_permutation_conditions(303))
print(check_permutation_conditions(1221))
False True True
Method 2: Optimized Approach with Early Exit
We can optimize by checking divisibility by 3 first, since all permutations have the same digit sum ?
from itertools import permutations
def optimized_check(n):
digits = str(n)
# First check if ANY permutation can be divisible by 3
digit_sum = sum(int(digit) for digit in digits)
if digit_sum % 3 != 0:
return False
# Now check for palindromic permutations
for perm in permutations(digits):
if perm[0] == '0': # Skip invalid numbers
continue
perm_str = ''.join(perm)
if perm_str == perm_str[::-1]: # Check palindrome
return True
return False
# Test cases
test_numbers = [121, 303, 1221, 102, 999]
for num in test_numbers:
result = optimized_check(num)
print(f"{num}: {result}")
121: False 303: True 1221: True 102: False 999: True
How It Works
The solution works in two phases:
- Divisibility Check: Since all permutations have the same digits, they all have the same digit sum. We check this once upfront.
- Palindrome Check: We generate permutations and check if any forms a palindrome (excluding numbers starting with 0).
Key Points
- All permutations of a number have the same digit sum, so divisibility by 3 is constant across permutations
- We must exclude permutations that start with 0 as they represent invalid numbers
- The optimized approach checks divisibility first to potentially exit early
Conclusion
To check if any permutation of a number is both divisible by 3 and palindromic, verify the digit sum is divisible by 3, then check if any valid permutation forms a palindrome. The optimized approach improves efficiency by checking divisibility first.
