Project Euler 52

Project Euler 52

题目

Permuted multiples

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.

解决方案

直接枚举。可以发现,如果要满足上面的条件,那么\(x\)\(6x\)的位数必须相同。

因此,对于一个\(n\)位数而言,只需要枚举这些\(n\)位数的前\(\dfrac{1}{6}\)的部分。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from itertools import count

for i in count(1, 1):
l = 10 ** (i - 1)
r = 10 ** i // 6
ok = False
for x in range(l, r + 1):
st = set()
for i in range(1, 7):
st.add("".join((lambda x: (x.sort(), x)[1])(list(str(x * i)))))
if len(st) == 1:
ok = True
ans = x
break
if ok:
break
print(ans)
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
Ujimatsu Chiya 微信 微信
Ujimatsu Chiya 支付宝 支付宝