princeofsudan

ProjectEuler#38 Pandigital multiples

Apr 5th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. #Take the number 192 and multiply it by each of 1, 2, and 3:
  2.  
  3. #    192 x 1 = 192
  4. #    192 x 2 = 384
  5. #    192 x 3 = 576
  6.  
  7. #By concatenating each product we get the 1 to 9 pandigital, 192384576.
  8. #We will call 192384576 the concatenated product of 192 and (1,2,3)
  9.  
  10. #The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4,
  11. #and 5, giving the pandigital, 918273645, which is the concatenated product
  12. #of 9 and (1,2,3,4,5).
  13.  
  14. #What is the largest 1 to 9 pandigital 9-digit number that can be formed as
  15. #the concatenated product of an integer with (1,2, ... , n) where n > 1?
  16.  
  17. def is1To9Pandigital(nStr):
  18.     if '0' in nStr or len(nStr) != 9:
  19.         return False
  20.     count = 0
  21.     for i in range(1,10):
  22.         if str(i) in nStr:
  23.             count +=1
  24.     if count == 9:
  25.         return True
  26.     else:
  27.         return False
  28.  
  29. largestProduct = 0
  30.  
  31. for i in range(1,1000000): #since n>2, max number should have 5-digits max
  32.     productStr = ''
  33.     for j in range(1,10):
  34.         productStr += str(i*j)
  35.         if len(productStr) >= 9:
  36.             break
  37.     if len(productStr) == 9 and is1To9Pandigital(productStr)\
  38.        and int(productStr) > largestProduct:
  39.         largestProduct = int(productStr)
Advertisement
Add Comment
Please, Sign In to add comment