Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once;
- # for example, the 5-digit number, 15234, is 1 through 5 pandigital.
- #The product 7254 is unusual, as the identity, 39 x 186 = 7254, containing multiplicand, multiplier, and product
- #is 1 through 9 pandigital.
- #Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9
- # pandigital.
- #HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
- def is1To9Pandigital(nStr):
- if '0' in nStr or len(nStr) != 9:
- return False
- count = 0
- for i in range(1,10):
- if str(i) in nStr:
- count +=1
- if count == 9:
- return True
- else:
- return False
- products = []
- for i in range(1,10): # 1 digit x 4 digits = min 4 digits, max 5 gigits
- for j in range(1,10000):
- product = i*j
- if is1To9Pandigital(str(i)+str(j)+str(product)):
- products.append(product)
- for i in range(1,100): # 2 digit x 3 digits = min 4 digits, max 5 gigits
- for j in range(1,1000):
- product = i*j
- if is1To9Pandigital(str(i)+str(j)+str(product)):
- products.append(product)
- products = set(products) #remove duplicates
- count = 0
- for i in products: #sum products
- count += i
- print count
Advertisement
Add Comment
Please, Sign In to add comment