Check if any permutation of a large number is divisible by 8 in Python

PythonServer Side ProgrammingProgramming

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
  • 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

 Live Demo

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
raja
Published on 30-Dec-2020 13:05:09
Advertisements