from functools import reduce def gen_fac(n): base = 1 for n in range(1, n + 2): yield base base *= n def find_sum(): s = set() fac = list(gen_fac(9)) for value in fac: if 3 > value: continue for x in range(value, value + 1000): if reduce(lambda x, y: x + fac[y], list(map(lambda x: int(x), str(x))), 0) == x: s.add(x) return s print(sum(find_sum()))