Problem 38 – Pandigital Products

by

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.

Advertisements

2 Responses to “Problem 38 – Pandigital Products”

  1. sumidiot Says:

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

    • Ace Says:

      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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s


%d bloggers like this: