問題
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.
例
入力:
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.
入力:
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 つずつ開いてみましょう。
ヒント 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用に調整されています。上記の説明と制約に従ってください。