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 [95]:
import numpy as np
def same_digits(x, y):
"""Do the integers x and y have the same digits, regardless of order."""
xx = str(x)
yy = str(y)
sum_1 = 0
sum_2 = 0
i = 0
mult_1 = 0
mult_2 = 0
while i < len(xx):
if len(xx) != len(yy):
return False
sum_1 += int(xx[i])
sum_2 += int(yy[i])
mult_1 = int(xx[i])*int(xx[i-1])
mult_2 = int(yy[i])*int(yy[i-1])
i+=1
mult_3 = mult_2 * int(yy[0])
mult_4 = mult_1 * int(xx[0])
if sum_1 == sum_2 and mult_4 == mult_3:
return True
else:
return False
"""My thoughts here were that the sum and product of all the numbers in the x and y woud be equal.
"""
same_digits('101', '202')
Out[95]:
In [86]:
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 [97]:
s = 0
while False:
s+=1
answer_1 = same_digits(s,2*s)
answer_2 = same_digits(s,3*s)
answer_3 = same_digits(s,4*s)
answer_4 = same_digits(s,5*s)
answer_5 = same_digits(s,6*s)
if answer_1 and answer_2 and answer_3 and answer_4 and answer_5 == True:
return True
if answer == True:
print(i)
"""I want to loop through every number and put them into same digits. """
Out[97]:
In [94]:
assert True # leave this cell to grade the solution
In [ ]: