
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if any permutation of a large number is divisible by 8 in Python
Suppose, we are provided with a huge number and we have to find out whether any permutation of the digits of the number is divisible by 8. The number is provided to us in string format.
So, if the input is like: input_num = 4696984, then the output will be “Divisible by eight”.
To solve this problem, we will check all the three-digit permutations possible with the digits of the number and see if they can occur in any all-digit permutation of the number. If a three-digit permutation divisible by eight occurs at the end of an all-digit permutation of the number, we will say that permutation is divisible by 8.
To solve this, we will follow these steps −
- if length of input_num < 3, then
- if input_num mod 8 is same as 0, then
- return True
- input_num := reverse of input_num
- if input_num mod 8 is same as 0, then
- return True
- return False
- if input_num mod 8 is same as 0, then
- temp_arr := a new list of size 10 initialized by 0s.
- for count in range 0 to size of input_num, do
- temp_arr[input_num[count] - 0] := temp_arr[input_num[count] - 0] + 1
- for count in range 104 to 999, increase by 8, do
- temp := count
- occurences := a new list of size 10 initialized by 0s.
- occurences[temp mod 10] := occurences[temp mod 10] + 1
- temp := temp / 10
- occurences[temp mod 10] := occurences[temp mod 10] + 1
- temp := temp / 10
- occurences[temp mod 10] := occurences[temp mod 10] + 1
- temp := count
- if occurences[temp mod 10] > temp_arr[temp mod 10], then
- go for next iteration
- temp := temp / 10
- if occurences[temp mod 10] > temp_arr[temp mod 10], then
- go for next iteration
- temp := temp / 10
- if occurences[temp mod 10] > temp_arr[temp mod 10], then
- go for next iteration
- return True
- return False
Let us see the following implementation to get better understanding −
Example
def solve(input_num): if len(input_num) < 3: if int(input_num) % 8 == 0: return True input_num = input_num[::-1] if int(input_num) % 8 == 0: return True return False temp_arr = 10 * [0] for count in range(0, len(input_num)): temp_arr[int(input_num[count]) - 0] += 1 for count in range(104, 1000, 8): temp = count occurences = 10 * [0] occurences[int(temp % 10)] += 1 temp = temp / 10 occurences[int(temp % 10)] += 1 temp = temp / 10 occurences[int(temp % 10)] += 1 temp = count if (occurences[int(temp % 10)] > temp_arr[int(temp % 10)]): continue temp = temp / 10 if (occurences[int(temp % 10)] > temp_arr[int(temp % 10)]): continue temp = temp / 10 if (occurences[int(temp % 10)] > temp_arr[int(temp % 10)]): continue return True return False if solve("4696984"): print("Divisible by eight") else: print("Not divisible by eight")
Input
4696984
Output
Divisible by eight
- Related Articles
- Check if any permutation of a number is divisible by 3 and is Palindromic in Python
- Check if any large number is divisible by 17 or not in Python
- Check if any large number is divisible by 19 or not in Python
- Check if a large number is divisible by 8 or not in C++
- Check if a large number is divisible by 20 in C++
- Check if a large number is divisible by 11 or not in C++
- Check if a large number is divisible by 25 or not in C++
- Check if a large number is divisible by 3 or not in C++
- Check if a large number is divisible by 5 or not in C++
- Check if a large number is divisible by 75 or not in C++
- Check if a large number is divisible by 9 or not in C++
- Check if a large number is divisible by 13 or not in C++
- Check if a large number is divisible by 11 or not in java
- Check if a large number is divisible by 3 or not in java
- C Program to check if a number is divisible by any of its digits

Advertisements