def digit_sum(n: int) -> int: return sum(map(int, str(n))) def is_digital_power_sum(base: int, power: int): if digit_sum(base ** power) == base: return True return False def get_nth_power_sum(n: int): power_sums = [] for base in range(2, 600): for power in range(2, 50): if is_digital_power_sum(base, power): power_sums.append(base ** power) power_sums = sorted(power_sums) return power_sums[n - 1] def euler_119(): assert(is_digital_power_sum(28, 4) == True) assert(get_nth_power_sum(2) == 512) assert(get_nth_power_sum(10) == 614656) return get_nth_power_sum(30) if __name__ == "__main__": solution = euler_119() print("e119.py: " + str(solution)) assert(solution == 248155780267521)