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

Strong Number

Check whether a number equals the sum of the factorials of its digits.

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

  • Split a number into its digits with % and /
  • Precompute small factorials once
  • Compare a computed total with the original number

समस्या

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

A strong number is a number that equals the sum of the factorials of its digits. For example, 145 is strong because 1! + 4! + 5! = 1 + 24 + 120 = 145. Mathematicians call these numbers *factorions*; in base 10 there are only four of them, but your program should test the definition rather than a list.

Implement strong_number(n).

Use this definition: take each decimal digit d of n, add up d! for all of them (with 0! = 1), and compare the total with n. So 0 is not strong, because 0! = 1.

Input

n: The number to test.

Output

Return true if the sum of the factorials of the digits of n equals n, otherwise false.

सीमाएँ

0 ≤ n ≤ 10^9

उदाहरण

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

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

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

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

व्याख्या

1! + 4! + 5! = 1 + 24 + 120 = 145, which is the number itself, so the answer is true.

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

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

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

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

व्याख्या

1! + 2! + 3! = 1 + 2 + 6 = 9, and 9 ≠ 123, so the answer is false.

कैसे हल करें

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

संकेत 1

Keep a copy of n, because you will change the working value while taking digits off.

संकेत 2

Only the factorials of 0 to 9 are ever needed, so build them once.

संकेत 3

Check what your loop does when n is 0: it has one digit.

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

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

संदर्भ

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

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

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

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

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

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