https://www.acmicpc.net/problem/4673
# 셀프 넘버
def d(n): # 생성자를 이용해 d(n) 구하기
result = n
for i in str(n):
result += int(i)
return result
non_self_num = set() # 셀프넘버가 아닌 수
for i in range(1,10001):
non_self_num.add(d(i)) # d(n)을 이용해 셀프넘버가 아닌 수를 구한다.
for i in range(1, 10001):
if i not in non_self_num: # 셀프넘버 출력
print(i)
def d(n):
# 생성자 n을 이용해 d(n)을 만드는 수식
n = n + sum( map(int, str(n)) )
return n
int로 들어온 n을 str으로 형변환을 시켜주면 문자열 즉, iterable이 되고 그걸 map 함수를 이용해 다시 int로 바꾸면 sum()함수를 이용해 각 자리 수의 합을 구할 수 있다.
셀프 넘버가 아닌 수를 저장해 셀프 넘버를 구한다.
set()은 중복된 값을 제거한다.