We shall say that an -digit
number is pandigital if it makes use of all the digits to exactly once; for example, the -digit number, , is through pandigital.
The product is unusual, as
the identity, ,
containing multiplicand, multiplier, and product is through pandigital.
Find the sum of all products whose multiplicand/multiplier/product
identity can be written as a
through pandigital.
HINT: Some products can be obtained in more than one way so be sure
to only include it once in your sum.
解决方案
先枚举第 个因数,后枚举第 个因数。再判断两个因数和积拼接后的长度是否会大于.
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from itertools import count
N = 1000 c = "123456789" st = set() for i inrange(1, N): for j in count(i + 1, 1): s = str(i) + str(j) + str(i * j) iflen(s) > 9: break t = "".join((lambda x: (x.sort(), x)[1])(list(s))) if t == c: st.add(i * j) ans = sum(st) print(ans)