Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #The number, 1406357289, is a 0 to 9 pandigital number because it is made up
- #of each of the digits 0 to 9 in some order, but it also has a rather
- #interesting sub-string divisibility property.
- #Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way,
- #we note the following:
- # d2d3d4=406 is divisible by 2
- # d3d4d5=063 is divisible by 3
- # d4d5d6=635 is divisible by 5
- # d5d6d7=357 is divisible by 7
- # d6d7d8=572 is divisible by 11
- # d7d8d9=728 is divisible by 13
- # d8d9d10=289 is divisible by 17
- #Find the sum of all 0 to 9 pandigital numbers with this property.
- import itertools
- def generatePerm(nDigits):
- #tupleList is sorted asc
- tupleList = list(itertools.permutations(range(nDigits),nDigits))
- intList = []
- for i in tupleList: #Convert tuples list generated to ints list
- digitStr = ''
- for j in i:
- digitStr += str(j)
- if int(digitStr[0])!= 0: #add to ints list only numbers not starting
- intList.append(int(digitStr)) #with 0
- return intList
- intList = generatePerm(10)
- divisableList = []
- count = 0
- for i in intList:
- if int(str(i)[1]+str(i)[2]+str(i)[3])%2 == 0 and\
- int(str(i)[2]+str(i)[3]+str(i)[4])%3 == 0 and\
- int(str(i)[3]+str(i)[4]+str(i)[5])%5 == 0 and\
- int(str(i)[4]+str(i)[5]+str(i)[6])%7 == 0 and\
- int(str(i)[5]+str(i)[6]+str(i)[7])%11 == 0 and\
- int(str(i)[6]+str(i)[7]+str(i)[8])%13 == 0 and\
- int(str(i)[7]+str(i)[8]+str(i)[9])%17 == 0:
- divisableList.append(i)
- count += i
- print divisableList
- print count
Advertisement
Add Comment
Please, Sign In to add comment