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]:
def same_digits(x, y):
"""Do the integers x and y have the same digits, regardless of order."""
z = []
w = []
n = 0
x = str(x)
y = str(y)
thing = True
while n < len(x):
z.append(x[n])
n = n+1
n = 0
while n < len(y):
w.append(y[n])
n = n+1
z = sorted(z)
w = sorted(w)
return z == w
#same_digits('132', '321')
In [3]:
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 [4]:
x = 0
while x < 10000000:
x = x + 1
if same_digits(x, 2*x) and same_digits(x, 3*x) and same_digits(x, 4*x) and same_digits(x, 5*x) and same_digits(x, 6*x):
z = x
break
print(z)
In [ ]:
assert True # leave this cell to grade the solution