def get_limit(num): n = 1 while (10 ** n) < n * (9 ** num): n += 1 return n def eq(x, num): ori_number = x sum_power = 0 while x: sum_power += (x % 10) ** num x //= 10 return sum_power == ori_number def get(num): for x in range(2, 10 ** get_limit(num)): if eq(x, num): yield x print(sum(get(5)))