Project Euler: Problem 52

https://projecteuler.net/problem=52

It can be seen that the number, $125874$, and its double, $251748$, contain exactly the same digits, but in a different order.

Find the smallest positive integer, $x$, such that $2x$, $3x$, $4x$, $5x$, and $6x$, contain the same digits.

First, write a function same_digits(x,y) that returns True if two integers x and y have the exact same set of digits and multiplicities and False if they have different digits.


In [26]:
def same_digits(x, y):
    """Do the integers x and y have the same digits, regardless of order."""
    test1 = True
    test2 = True
    for a in x:
        if a not in y:
            test1 = False
            break
    for b in y:
        if b not in x:
            test2 = False
            break
    test = test1 and test2
    return test

In [27]:
assert same_digits('132', '321')
assert not same_digits('123', '3')
assert not same_digits('456', '0987654321')

Now use the same_digits function to solve this Euler problem. As you work on this problem, be careful to debug and test your code on small integers before trying it on the full search.


In [28]:
def tester(x):
    a = str(x)
    b = str(2*x)
    c = str(3*x)
    d = str(4*x)
    e = str(5*x)
    f = str(6*x)
    u = same_digits(a, b)
    v = same_digits(b, c)
    w = same_digits(c, d)
    y = same_digits(d, e)
    z = same_digits(e, f)
    if(u and v and w and y and z):
        return True
    else:
        return False
    

for x in range(1,1000000):
    if tester(x) == True:
        print(x)


142857

In [31]:
142857
142857*2
142857*3
142857*4
142857*5
142857*6


Out[31]:
857142

In [29]:
assert True # leave this cell to grade the solution