Back to challenges
Easy 0 solved 0% acceptance

Classify a Number by Its Divisors

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

What you'll learn

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

Problem

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.

Constraints

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

Examples

Example 1
Input:
n = 18

Expected output:
expected = "abundant"
Input:
long long n = 18;

Expected output:
std::string expected = "abundant";
Input:
long n = 18;

Expected output:
string expected = "abundant";

Explanation

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

Example 2
Input:
n = 28

Expected output:
expected = "perfect"
Input:
long long n = 28;

Expected output:
std::string expected = "perfect";
Input:
long n = 28;

Expected output:
string expected = "perfect";

Explanation

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

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

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

Hint 2

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

Hint 3

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

Expected complexity

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

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.