[백준/파이썬] 14782번 풀이

업데이트:



문제 정보


풀이

문제

Farmer John was performing his nightly bedtime reading duties for Bessie. “Oh, this gives me a headache,” moaned Bessie. “But it’s just simple number theory,” replied FJ. “Let’s go over it again. The sigma function of a number is just the sum of the divisors of the number. So, for a number like 12, the divisors are 1, 2, 3, 4, 6, and 12. Summing them we get 28.” “That’s all there is to it?” asked Bessie. “Yep.” replied FJ. “Perhaps someone will write a program to calculate the sigma function of an integer I (1 <= I <= 1,000,000).”

입력 요약
A single integer I.

출력 요약
Output a line containing the sum of all of I’s divisors.

코드

s,n=0,int(input())
for i in range(1,int(n**.5)+1):
    if n%i==0:
        s+=i
        if i*i!=n:s+=n//i
print(s)

설명

핵심은 구현 관점에서 Farmer John was performing his nightly bedtime reading duties for Bessie. “Oh, this gives me a headache,” moaned Bessie. …를 만족하도록 로직을 구성하는 것입니다.

코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.

경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.



다음 읽을거리

관련 허브 페이지에서 같은 주제의 글을 이어서 확인할 수 있습니다.

댓글남기기