आसान 0 हल किए गए 0% स्वीकृति

Abundant, Perfect or Deficient

Classify a number as abundant, perfect or deficient by the sum of its proper divisors.

आप क्या सीखेंगे

  • Sum proper divisors in O(√n) time
  • Avoid counting a square root divisor twice
  • Return one of three labels from a comparison

समस्या

अनुवाद दिखाएँ

The proper divisors of n are its positive divisors other than n itself. Compare their sum with n: if the sum is larger the number is abundant, if it is equal the number is perfect, and if it is smaller the number is deficient. For example, the proper divisors of 12 are 1, 2, 3, 4 and 6, which add up to 16, so 12 is abundant.

Implement abundant_perfect_deficient(n).

n can be as large as 10^12, so checking every number below n is far too slow. Divisors come in pairs d and n / d, so you only need to try d up to √n.

Input

n: The number to classify.

Output

Return the string "abundant", "perfect" or "deficient" in lowercase. 1 has no proper divisors (sum 0), so it is deficient.

सीमाएँ

1 ≤ n ≤ 10^12

उदाहरण

उदाहरण 1
इनपुट:
n = 28

अपेक्षित आउटपुट:
expected = "perfect"
इनपुट:
long long n = 28;

अपेक्षित आउटपुट:
std::string expected = "perfect";
इनपुट:
long n = 28;

अपेक्षित आउटपुट:
string expected = "perfect";

व्याख्या

The proper divisors 1 + 2 + 4 + 7 + 14 add up to 28, so the answer is "perfect".

उदाहरण 2
इनपुट:
n = 15

अपेक्षित आउटपुट:
expected = "deficient"
इनपुट:
long long n = 15;

अपेक्षित आउटपुट:
std::string expected = "deficient";
इनपुट:
long n = 15;

अपेक्षित आउटपुट:
string expected = "deficient";

व्याख्या

The proper divisors 1 + 3 + 5 add up to 9, which is less than 15, so the answer is "deficient".

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

If d divides n, so does n / d; loop d only while d × d ≤ n.

संकेत 2

When d × d = n, add d once, not twice.

संकेत 3

Remember to leave n itself out of the sum, and handle n = 1 separately.

अपेक्षित जटिलता

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

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।