모든 챌린지
쉬움 1명 해결 정답률 100.0%

Classify a Number by Its Divisors

Classify a positive integer as perfect, abundant, or deficient.

배우게 될 내용

  • Find all proper divisors of a number
  • Speed up divisor search with the square root
  • Classify results with conditions

문제

번역본 보기

Add the positive divisors of n that are strictly smaller than n. If their sum equals n, the number is perfect. If the sum is larger, it is abundant; if smaller, it is deficient. The number 1 has no proper positive divisors, so its divisor sum is 0.

Implement perfect_number(n).

Input

n: The positive integer to classify.

Output

Return perfect, abundant, or deficient as lowercase text.

제약 조건

1 ≤ n ≤ 1,000,000. Zero and negative values are excluded.

예시

예시 1
입력:
n = 18

예상 출력:
expected = "abundant"
입력:
long long n = 18;

예상 출력:
std::string expected = "abundant";
입력:
long n = 18;

예상 출력:
string expected = "abundant";

설명

The proper divisors are 1, 2, 3, 6, and 9. Their sum is 21, greater than 18.

예시 2
입력:
n = 28

예상 출력:
expected = "perfect"
입력:
long long n = 28;

예상 출력:
std::string expected = "perfect";
입력:
long n = 28;

예상 출력:
string expected = "perfect";

설명

The proper divisors sum to 1 + 2 + 4 + 7 + 14 = 28, exactly the number itself.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Find the proper divisors of n and compare their sum with n to decide the class.

힌트 2

You only need to search divisors up to the square root, pairing each divisor with its companion.

힌트 3

Treat 1 carefully, since it has no proper positive divisors and its divisor sum is zero.

예상 복잡도

O(√n) time, O(1) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 KKKaneki Ken Python 1,167 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 솔루션

스포일러: 아래에 1개 전체 솔루션 있음 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.