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 large number is divisible by 8 in Python
A number is divisible by 8 if its last three digits form a number divisible by 8. For large numbers, checking all permutations is impractical, but we can use this divisibility rule to efficiently check if any permutation is divisible by 8.
Divisibility Rule for 8
According to the divisibility rule of 8, if the last three digits of a number are divisible by 8, then the entire number is divisible by 8. We can solve this problem by checking if we can form any three-digit combination from the given digits that is divisible by 8.
Examples
Scenario 1
<b>Input:</b> "61" <b>Output:</b> Yes <b>Explanation:</b> The possible permutations are 16 and 61. Since 16 is divisible by 8 (16/8=2), the answer is Yes.
Scenario 2
<b>Input:</b> "111" <b>Output:</b> No <b>Explanation:</b> The only permutation is 111. Since 111/8 = 13.875, it's not divisible by 8.
Approach for Small Numbers (? 3 digits)
For numbers with 3 or fewer digits, we can generate all permutations and check divisibility ?
from itertools import permutations
def check_small_number(num_str):
perms = set(permutations(num_str))
for perm in perms:
number = int(''.join(perm))
if number % 8 == 0:
return "Yes"
return "No"
# Test with small numbers
test_cases = ["61", "111", "124"]
for test in test_cases:
result = check_small_number(test)
print(f"Input: {test}, Output: {result}")
Input: 61, Output: Yes Input: 111, Output: No Input: 124, Output: Yes
Optimized Approach for Large Numbers
For larger numbers, we generate all three-digit numbers divisible by 8 and check if we can form them using available digits ?
from collections import Counter
def check_divisibility_by_8(num_str):
digit_length = len(num_str)
# For small numbers, check all permutations
if digit_length <= 3:
from itertools import permutations
perms = set(permutations(num_str))
for perm in perms:
number = int(''.join(perm))
if number % 8 == 0:
return "Yes"
return "No"
# For large numbers, use the optimized approach
# Generate all 3-digit numbers divisible by 8
divisible_by_8 = [str(i).zfill(3) for i in range(0, 1000, 8)]
# Count available digits
available_digits = Counter(num_str)
# Check if we can form any of these numbers
for three_digit in divisible_by_8:
required_digits = Counter(three_digit)
# Check if we have enough digits to form this number
if all(available_digits[digit] >= required_digits[digit] for digit in required_digits):
return "Yes"
return "No"
# Test with various inputs
test_cases = ["61", "111", "31462", "1234567890"]
for test in test_cases:
result = check_divisibility_by_8(test)
print(f"Input: {test}, Output: {result}")
Input: 61, Output: Yes Input: 111, Output: No Input: 31462, Output: Yes Input: 1234567890, Output: Yes
How the Algorithm Works
The algorithm uses two different strategies based on input size:
- Small numbers (? 3 digits): Generate all permutations and check each one
- Large numbers (> 3 digits): Generate all possible 3-digit numbers divisible by 8 (000, 008, 016, ..., 992) and check if we can form any of them using the available digits
Time Complexity
| Approach | Time Complexity | Best For |
|---|---|---|
| All Permutations | O(n! × n) | Small numbers (? 3 digits) |
| Divisibility Rule | O(125) | Large numbers (> 3 digits) |
Conclusion
This approach efficiently determines if any permutation of a large number is divisible by 8 using the divisibility rule and Python's Counter. For large inputs, it avoids generating all permutations and runs in constant time.
