Not long ago, a member posted a problem in the Homework section, concerned with determining which of the five-digit permutations of 4, 5, 6, 8, and 9 were divisible by 8.(adsbygoogle = window.adsbygoogle || []).push({});

I first thought about writing some C code to figure this out, but when I discovered that Python has some functions that would make this problem a lot easier, I abandoned my attempt using C. The permutations() and combinations() functions in theitertoolsmodule, are very useful in this problem.

I don't know much Python, so I used this problem as an exercise in becoming more knowledgeable with this language. If you have any comments on my code, feel free.

Here's the solution I came up with. BTW, my program reports that the are 20 permutations that are divisible by 8, and it lists them. There are several commented-out print statements that I used for debugging purposes. I left them in place, but commented-out, instead of removing them.

Code (Python):

# plates.py

# Find all permutions of 4, 5, 6, 8, and 9 that are divisible by 8.

# I'm using license plates with five numeric digits as an analogy here.

# Algorithm:

# Construct all possible permutations of the set (4, 5, 6, 8, 9}.

# For each permutation tuple, construct in integer whose digits are generated from the given tuple.

# Strip off the two highest digits of the integer, leaving a three-digit integer.

# If the three-digit integer is divisible by 8, the five-digit number it came from is also divisible by 8.

# Display each of the five-digit numbers that are divisible by 8, together with how many there are.

import sys

import itertools

digit_set = ['4', '5', '6', '8', '9']

TotalCount = 0 # Count of all plates

Count8 = 0 # Count of plates with numbers that are divisible by 8

NumPlate = 0 # An individual license plate, as an integer

Last_three = 0

# Make a collection of plates with the five digits in the list above.

plates = itertools.permutations(digit_set, 5)

# Iterate through each item in the plates collection.

for plate in plates:

TotalCount = TotalCount + 1

#print('Plate', plate)

NumPlate = 0

# A plate is a tuple of characters (actually, strings of length 1).

# Construct a number out of the digit characters in each tuple.

for ltr in plate:

#print("Letter: ", ltr)

NumPlate = 10 * NumPlate + int(ltr)

#print('Number plate: ', NumPlate)

# Strip off the first two digits.

# If the last three digits of an integer are divisible by 8, the integer is divisible by 8.

# E.g., 1008 is divisible by 8, but 1009 is not.

Last_three = NumPlate % 1000

#print('Stripped digits: ', Last_three)

if Last_three % 8 == 0:

print('Last three digits: ', Last_three)

print('Plate number: ', NumPlate)

Count8 = Count8 + 1

else:

continue

print('Done')

print('Count of all permutations', TotalCount)

print('Count of permutations divisible by 8: ', Count8)

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Program to list specific permutations of 5 digits

**Physics Forums | Science Articles, Homework Help, Discussion**