簡単 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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。