'''
September Math Puzzler
Obtain a nine-digit number consisting of each of the digits 1 - 9
(each exactly once) so that the nine conditions below are satisfied:
The number is divisible by 9; The number obtained by omitting the
rightmost digit in this number is divisible by 8; The number obtained by
omitting the two rightmost digits in this number is divisible by 7; ...
The number obtained by omitting the eight rightmost digits in this number
is divisible by 1.
'''
import itertools as i
def makeNumber(dig):
'''
Make an integer from a tuple of 9 digits
'''
num = 0
for n in range(9):
num += dig[n] * 10**n
return num
def checkNumber(num):
'''
Test the number to see if it meets the requirements of of the first digit
being evenly divisible by 1, the first 2 digits are evenly divible by 2, ...,
and all whole int divisible by 9
'''
flag = True #Flag to indicate one of the divisions failed
# to be evenly divisible
for numberOfDigits in range(1,10):
#First isolation the digits to be checked
divisor = 10**(9-numberOfDigits)
dividend = num / divisor
#Now see if it is evenly divisible by the number of digits
if dividend % (numberOfDigits) != 0:
flag = False #Any "non-multiple" fails
break
if flag: #If all the division were evenly divisible
print num # then we found a solution
if __name__ == "__main__":
#Make a list of all the possible numbers using digits 1->9, each only
# once, in any position.
d=list(i.permutations(range(1,10)))
#Check each one to see if it meets the requirements
for n in d:
checkNumber(makeNumber(n))