In problem 38 we’re supposed to find the biggest 9 digit pandigital number that is obtained by concatenating consecutive multiples of a fixed number, starting with the number itself. Sorta a mouthful, but the examples in the problem text make it pretty clear, I think.
10 minutes thought, or so, might convince you that (if the answer isn’t the example given in the problem text) the number you’ll be taking multiples of must be a four digit number, bigger than 9182. So you really don’t have too much to loop through, which is nice. Here’s what I came up with:
def solve():
biglhs = 9182
digits = map(str, xrange(1,10))
for lhs in xrange(9183,10000):
need = digits[:] # make a copy
# get rid of digits in the lhs
for d in str(lhs):
if need.count(d): need.remove(d)
else: break # quit early, MASSIVE speed improvement
# if lhs is unique digits, and rhs is the remaining digits
if len(need) == 5 and need == sorted(map(str, str(2*lhs))):
biglhs = lhs
biggest = "%s%s"%(biglhs,2*biglhs)
return biggest
Quick problems are fun.
Advertisement
February 26, 2010 at 5:56 pm |
idiot. loop down from lhs=9999, stop when you find the first pandigital number (lhs)(2*lhs).