К списку задач
Легко Решено: 0 Процент решений: 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. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.