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 [2]:
import numpy as np

In [6]:
def same_digits(x, y):
    """Do the integers x and y have the same digits, regardless of order."""
    xlist = np.array(list(str(x)))
    ylist = np.array(list(str(y)))
    for ch in xlist:
        if ch not in ylist:
            return False
    for ch in ylist:
        if ch not in xlist:
            return False
    return True

In [ ]:


In [7]:
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 [ ]:
for x in range(1,10000000):
    if same_digits(x, 2*x):
        if same_digits(2*x, 3*x):
            if same_digits(3*x, 4*x):
                if same_digits(4*x, 5*x):
                    if same_digits(5*x, 6*x):
                        print(x)
                    else:
                        print("DUUUUUUDE! So close, but no cigar. Try a bigger number")
                else:
                    print("try a bigger number lvl 4")
            else:
                print("try a bigger number lvl 3")
        #else:
            #print("try a bigger number lvl 2")
    #else:
        #print("try a bigger number lvl 1")


142857
try a bigger number lvl 3

In [24]:



142857
285714
428571
571428
714285
857142

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