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

Disarium Number

Check whether a number equals the sum of its digits raised to their positions.

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

  • Read digits from left to right
  • Raise a digit to its position with integer arithmetic
  • Add powers without overflow

समस्या

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

Number the digits of n from the left starting at 1, raise each digit to the power of its position, and add the results. If the total is n itself, the number is a disarium number. For example, 175 gives 1¹ + 7² + 5³ = 1 + 49 + 125 = 175.

Implement disarium_number(n).

Input

n: The number to test.

Output

Return true if n is a disarium number, otherwise false. Every single-digit number is a disarium number.

सीमाएँ

1 ≤ n ≤ 10^9

उदाहरण

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

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

अपेक्षित आउटपुट:
bool expected = true;
इनपुट:
long n = 89;

अपेक्षित आउटपुट:
bool expected = true;

व्याख्या

8¹ + 9² = 8 + 81 = 89, so the answer is true.

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

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

अपेक्षित आउटपुट:
bool expected = false;
इनपुट:
long n = 98;

अपेक्षित आउटपुट:
bool expected = false;

व्याख्या

9¹ + 8² = 9 + 64 = 73, not 98, so the answer is false.

कैसे हल करें

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

संकेत 1

Positions count from the left, so either turn n into a string or count the digits first.

संकेत 2

Use integer multiplication for powers instead of a floating point function.

संकेत 3

The largest term is 9^10, which fits easily in 64 bits.

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

O(d^2) time for d digits, O(1) extra space

संदर्भ

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

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

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

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

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

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