from math import log10 def pick(x, lis, out, a = 0): if x == 0: out.append([a, lis]) return a *= 10 for i in xrange(len(lis)): tmp = lis[:] tmpa = a + lis[i] tmp.pop(i) pick(x - 1, tmp, out, tmpa) def test(x, n): tmp = x[:] while n > 0: if tmp.count(n % 10): tmp.remove(n % 10) n /= 10 else: return False if len(tmp) > 0: return False return True total = [] tt = [] pick(1, [1,2,3,4,5,6,7,8,9], tt) for i in tt: yy = [] pick(4, i[1], yy) for j in yy: if test(j[1], i[0] * j[0]): tmp = i[0] * j[0] if total.count(tmp) == 0: total.append(tmp) tt = [] pick(2, [1,2,3,4,5,6,7,8,9], tt) for i in tt: yy = [] pick(3, i[1], yy) for j in yy: if test(j[1], i[0] * j[0]): tmp = i[0] * j[0] if total.count(tmp) == 0: total.append(tmp) print sum(total)