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 [1]:
def same_digits(x, y):
    value = True
    xs = str(x)
    ys = str(y)
    
    xl = [i for i in xs]
    yl = [j for j in ys]
    
    xs = sorted(xl)
    ys = sorted(yl)
    
    if len(xs) != len(ys):
        value = False
    else:
        for i in range(0,len(xs)):
            if xs[i]!=ys[i]:
                value = False
    return value

In [2]:
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.

Searching for the smallest. So I will look from the bottom and the first one I find is the smallest


In [6]:
myint = 0
for b in range(1,200000):
    if same_digits(b, 2*b) and same_digits(b, 3*b) and same_digits(b, 4*b) and same_digits(b, 5*b) and same_digits(b, 6*b)  :
        myint = b
        break
myint


Out[6]:
142857

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