Problem #52

Project Euler Prob­lem #52 was inter­est­ing and easy to solve.  The prob­lem states:

Find the small­est pos­i­tive inte­ger, x, such that 2x, 3x, 4x, 5x, and 6x, con­tain the same digits.

My method of attack was to incre­ment through the pos­i­tive inte­gers cal­cu­lat­ing the above prod­ucts for each.  The prod­ucts were then con­verted into a string, bro­ken into a list of char­ac­ters, and finally sorted. The sorted ver­sion of the lists for each prod­uct were then com­pared to see if they were all the same. Python code is below.

#!/usr/bin/python

import sys

for x in range(126000,250000):
    a2 = list(str(x*2)); a2.sort()
    a3 = list(str(x*3)); a3.sort()
    a4 = list(str(x*4)); a4.sort()
    a5 = list(str(x*5)); a5.sort()
    a6 = list(str(x*6)); a6.sort()

    if a2 == a3 == a4 == a5 == a6:
        print “True! x=”+str(x)
        print “a2 “+.join(a2)+” “+str(x*2)
        print “a3 “+.join(a3)+” “+str(x*3)
        print “a4 “+.join(a4)+” “+str(x*4)
        print “a5 “+.join(a5)+” “+str(x*5)
        print “a6 “+.join(a6)+” “+str(x*6)
    else:
        if x % 500 == 0:
            sys.std­out.write(”.”)
print “Com­plete”

Leave a Reply