38.py 956 B

123456789101112131415161718192021222324252627282930313233343536
  1. # Set to check if a number is pandigital
  2. pan = set("123456789")
  3. # Not used in this problem but may come in handy
  4. # def setmaker(n):
  5. # return set(x for x in str(n))
  6. def concatProduct(n, pan):
  7. """
  8. Input is a fixed number and a list, function will return concatenated
  9. product of the fixed number with individual elements of the list
  10. """
  11. product = ""
  12. for i in pan:
  13. product += str(n*i)
  14. return product
  15. def isPandigital(n):
  16. """Returns a boolean indicating whether a number is pandigital"""
  17. nset = set(str(n))
  18. if nset == pan and len(n) == 9:
  19. return True
  20. else:
  21. return False
  22. maxProduct = 918273645
  23. # Main loop, solution set contained to 4 digit numbers that are greater than the
  24. # result given in the prompt by 9 and (1,2,3,4,5)
  25. for fixed in range(9000,10000):
  26. concatenatedProduct = concatProduct(fixed, (1,2))
  27. if isPandigital(concatenatedProduct):
  28. if concatenatedProduct > maxProduct:
  29. maxProduct = concatenatedProduct
  30. print maxProduct