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

Factorial Digit Sum

Add up the decimal digits of n factorial.

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

  • Multiply a digit array by a small number
  • Sum the digits of a large number
  • Remember that 0! = 1

समस्या

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

n! = 1 × 2 × ... × n grows faster than any power: 25! already has 26 digits. Compute n! exactly and add up its digits. Without big integers, store the number as an array of digits and multiply it by 2, 3, ..., n in turn.

Implement factorial_digit_sum(n).

Input

n: The number whose factorial is taken.

Output

Return the sum of the decimal digits of n!. Note that 0! = 1.

सीमाएँ

0 ≤ n ≤ 1000

उदाहरण

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

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

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

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

व्याख्या

5! = 120 and 1 + 2 + 0 = 3.

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

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

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

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

व्याख्या

0! is 1 by definition, so the answer is 1.

कैसे हल करें

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

संकेत 1

Start from 1 and multiply by 2, 3, ..., n.

संकेत 2

Multiply each digit, add the carry, keep the last digit and carry the rest.

संकेत 3

When the carry is still left over at the end, it becomes new digits.

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

O(n × D) time where D is the digit count of n!, O(D) extra space

संदर्भ

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

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

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

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

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

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