Euler Solution 32

From ProgSoc Wiki

Jump to: navigation, search

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

Solutions

Python by Althalus

Runtime: 1.5 seconds

def isPan(a,b,c):
        list = []
        for char in str(a):
                if char not in list: list.append(char)
        for char in str(b):
                if a not in list: list.append(char)
        for char in str(c):
                if char not in list: list.append(char)
        if len(list) == 9:
                for i in range(1,10):
                        if str(i) not in list:
                                return False
                return True
        return False

list = []
total = 0
#sqrt(987654321) = 32426. By the time we pass this, we'll have covered everything.
for a in range(1,10):
        for b in range(1,10000):
                c = a*b
                if panLength(a,b,c) == 9:
                        if isPan(a,b,c):
                                if c not in list:
                                        print a,b,c
                                        list.append(c)
                #if panLength(a,b,c) > 9: break
for a in range(1,100):
        for b in range(1,1000):
                c = a*b
                if panLength(a,b,c) == 9:
                        if isPan(a,b,c):
                                if c not in list:
                                        #print c
                                        list.append(c)
                #if panLength(a,b,c) > 9: break

for num in list:
        total =total+ num
print total
Personal tools