Factorial Digit Sum
Let S(m) denote the sum of the decimal digits of a positive integer m. Compute S(100!).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
\(n!\) means \(n \times (n - 1) \times \cdots \times 3 \times 2 \times 1\).
For example, \(10! = 10 \times 9 \times \cdots \times 3 \times 2 \times 1 = 3628800\),
and the sum of the digits in the number \(10!\) is \(3 + 6 + 2 + 8 + 8 + 0 + 0 = 27\).
Find the sum of the digits in the number \(100!\).
Problem 20: Factorial Digit Sum
Mathematical Development
Definition 1 (Digit sum). For a positive integer with digits and , define .
Theorem 1 (Digit count of ). The number of digits of is
Proof. A positive integer has digits. Applying the multiplicative property of logarithms: .
Corollary 1. By Stirling’s approximation, . For : , so .
Theorem 2 (Digit sum congruence). For every positive integer , .
Proof. Since , we have for all , hence .
Theorem 3 (Legendre’s formula). For a prime and positive integer , the -adic valuation of is
Proof. Each factor contributes to . The number of multiples of in is . By inclusion, . (The sum is finite since for .)
Proposition 1 (). We have .
Proof. Direct application of Theorem 3 with . Since , we have , and by Theorem 2, .
Verification. , confirming .
Theorem 4 (Trailing zeros of ). The number of trailing zeros of is .
Proof. A trailing zero corresponds to a factor of . Since for all (as multiples of 2 are more frequent than multiples of 5), the count of trailing zeros equals .
Corollary 2. . Hence has 24 trailing zeros, which contribute nothing to .
Proposition 2 (Bounds on ). Since has 158 digits:
The 134 non-trailing-zero digits have an average value near , giving a heuristic estimate . The actual value is modestly above this estimate.
Proof. Lower bound: at least one digit is nonzero. Upper bound: each digit .
Editorial
We form by multiplying the integers from 2 through , then extract its decimal digits and add them. The second phase repeatedly takes the last digit with a modulo-10 operation and removes it with integer division until no digits remain. This is sufficient because every digit of is processed exactly once.
Pseudocode
Algorithm: Digit Sum of a Factorial
Require: An integer n ≥ 1.
Ensure: The sum of the decimal digits of n!.
1: Initialize F ← 1.
2: For each k in {2, 3, ..., n}, update F ← F · k.
3: Compute the decimal digit expansion of F.
4: Return the sum of those digits.
Complexity Analysis
Proposition 3 (Time complexity). The algorithm runs in time.
Proof. Let by Stirling’s approximation. At step , the running product has digits, and multiplying it by (a number with digits) costs using schoolbook multiplication. The total time is:
The final digit-extraction loop runs in , which is dominated by the multiplication phase.
Proposition 4 (Space complexity). The algorithm uses space.
Proof. The intermediate product requires digits of storage.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> digits = {1};
for (int n = 2; n <= 100; n++) {
int carry = 0;
for (int j = 0; j < (int)digits.size(); j++) {
int val = digits[j] * n + carry;
digits[j] = val % 10;
carry = val / 10;
}
while (carry > 0) {
digits.push_back(carry % 10);
carry /= 10;
}
}
int sum = 0;
for (int d : digits) sum += d;
cout << sum << endl;
return 0;
}
"""Project Euler Problem 20: Factorial Digit Sum"""
import math
def main():
print(sum(int(d) for d in str(math.factorial(100))))