Project Euler Problem #32:

Problem #32 is one of the many problems on Project Euler that concerns permutations of strings of text or numbers. The question reads:

Project Euler Problem 32: Pandigital products
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 x 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.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

If you expected a solution that does not involve brute force, I’m sorry, but you’re probably going to be disappointed. Here’s my solution:

Solution #1: Brute Force Approach

We simply generate all permutations of the digits {1,2,3,4,5,6,7,8,9} and then iterate through each of them to find which can be written as a product equation. There are only two possibilities. Either the product will have a 1-digit number multiplied by a 4-digit number resulting in a 4-digit number or it will have a 2-digit number multiplied by a 3-digit number resulting in a 4-digit number. Here is an implementation of this method in Python 2.7:

 '''
 Author: Walker Kroubalkian
 Brute Force Approach to Project Euler Problem #32
 '''

 import time

 def listPermutations(myList):
     if(len(myList)==1):
         return [myList]
     a = myList[0]
     b = listPermutations(myList[1:])
     total = []
     for x in b:
         for c in range(len(x)+1):
             temp = x[0:c]
             temp.append(a)
             temp.extend(x[c:])
             total.append(temp)
     return total

 def projectEulerProblemThirtyTwo(myList):
     myList = listPermutations(myList)
     total = []

     for a in myList:
         b = 10*a[0]+a[1]         
         c = 100*a[2]+10*a[3]+a[4]         
         d = 1000*a[5]+100*a[6]+10*a[7]+a[8]
         if(b*c==d):             
             if d not in total:                 
                 total.append(d)         
         b = a[0]         
         c = 1000*a[1]+100*a[2]+10*a[3]+a[4]
         d = 1000*a[5]+100*a[6]+10*a[7]+a[8]         
         if(b*c==d):
             if d not in total:
                 total.append(d)

     final = 0
     for x in total:
         final+=x
     return final

 start = time.time()
 print projectEulerProblemThirtyTwo([1,2,3,4,5,6,7,8,9])
 print ("--- %s seconds ---" % (time.time()-start))

 '''
 Prints

 45228
 --- 0.590600013733 seconds ---

 for input of [1,2,3,4,5,6,7,8,9]
 '''

This solution is fairly efficient as there are only 9! = 362,880 permutations of the digits 1-9 but it could still be optimized. I may return to this question eventually to search for a more efficient solution.

Thanks for reading!

Published by Walker Kroubalkian

My name is Walker Kroubalkian. I really enjoy math, computer science, and hiking.

Leave a comment

Design a site like this with WordPress.com
Get started