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 [17]:
def same_digits(x, y):
"""Do the integers x and y have the same digits, regardless of order."""
x = str(x)
y = str(y)
X = []
Y = []
for char in x:
X.append(int(char))
for char in y:
Y.append(int(char))
A = sorted(X)
B = sorted(Y)
if A == B:
return True
else:
return False
In [18]:
same_digits(543, 546)
Out[18]:
In [19]:
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 [72]:
def mult_dig(n):
m=1
o = n*m
while same_digits(o, m) == False:
m+= 1
o += n
return m, o
In [73]:
assert mult_dig(2) == (125874, 251748)
In [74]:
def all_dig(n):
# i started out making this a function but
# since time is restrained and I'm just trying to solve up to 6x
# argument (n) doesn't do anything
m = 1
o = 2*m
p = 3*m
q = 4*m
r = 5*m
s = 6*m
while same_digits(o,m) == False or same_digits(m, p) == False or same_digits(m, q) == False or same_digits(m, r)==False or same_digits(m, s)==False:
m += 1
o += 2
p += 3
q += 4
r += 5
s += 6
# The problem only asks to find x (m in this)
# But o,p,q,r,s can be printed to see other values
print(m)
In [75]:
all_dig(1)
In [ ]:
assert True # leave this cell to grade the solution