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.

### Like this:

Like Loading...

*Related*

This entry was posted on February 14, 2010 at 11:44 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

February 26, 2010 at 5:56 pm |

idiot. loop down from lhs=9999, stop when you find the first pandigital number (lhs)(2*lhs).

December 21, 2012 at 9:09 pm |

And why would you do that, when you know the concatenation of 9999 to anything won’t be pandigital 1-9 because there’s already 4 9’s there…