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 [14]:
def y_sorter(y):
    ysort=ysort=''.join(sorted(([i for i in str(y)])))
    return ysort
y_sorter(125874)


Out[14]:
'124578'

In [26]:
def x_sorter(x):
    xsort=''.join(sorted(([i for i in str(x)])))
    return xsort
x_sorter(251748)


Out[26]:
'124578'

In [27]:
def same_digits(x, y):
    """Do the integers x and y have the same digits, regardless of order."""
    xsort=x_sorter(x)
    ysort=x_sorter(y)
    if ysort==xsort:
        return True
    return False

In [28]:
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 [49]:
def same_mult(x):    
    one=same_digits(x,2*x)
    two=same_digits(3*x,4*x)
    three=same_digits(5*x,6*x)
#     print(one,two,three)
    if one==True and one==two and two==three:
        return True
    else:
        return 'No'
    
same_mult(9999)


Out[49]:
'No'

In [59]:
def final(x):
    for x in range(1,x):
        value=same_mult(x)
        if value==True:
            print(x)
            break
        else:
            return('Fail')


Out[59]:
'Fail'

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