Digit Factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: As 1! = 1 and 2! = 2 are not sums, they...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
\(145\) is a curious number, as \(1! + 4! + 5! = 1 + 24 + 120 = 145\).
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Problem 34: Digit Factorials
Mathematical Development
Definition 1. For a positive integer with decimal representation (where ), define the digit factorial sum
A positive integer is called a factorion if .
Theorem 1 (Upper bound on factorions). If is a factorion, then . Equivalently, every factorion has at most digits.
Proof. Let have digits. Then (for ) and . A necessary condition for is , i.e.,
We evaluate for critical values:
| 7 | |||
| 8 |
Claim. for all .
Proof of claim. We proceed by induction. Base case: . Inductive step: assume for some . Then
For , , so .
Therefore no -digit number with can be a factorion. Since for any 7-digit number, the search space is .
Lemma 1 (Precomputation). The digit factorials are:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 2 | 6 | 24 | 120 | 720 | 5040 | 40320 | 362880 |
Using this lookup table, can be computed in time.
Proof. Extract digits by repeated division by 10, summing the looked-up factorials. The number of divisions equals the number of digits.
Theorem 2 (Complete classification). The only factorions with are and .
Proof. By Theorem 1, it suffices to evaluate for all and check . This exhaustive computation identifies exactly two solutions:
- .
- .
No other value in the search range satisfies the factorion condition.
Editorial
We perform a bounded exhaustive search using the upper bound established in the mathematical development. The factorials of the digits 0 through 9 are precomputed once, then every candidate from 3 up to is tested by summing the factorials of its decimal digits. Whenever this digit-factorial sum equals the original number, the candidate is added to the running total.
Pseudocode
Algorithm: Sum of Numbers Equal to the Sum of Factorials of Their Digits
Require: The decimal digit set {0, 1, ..., 9}.
Ensure: The sum of all integers equal to the sum of the factorials of their decimal digits.
1: Precompute f(d) ← d! for each digit d.
2: Initialize total ← 0.
3: For each candidate n in {3, 4, ..., 7 · 9!}, compute s ← sum of f(d) over the decimal digits d of n.
4: If s = n, update total ← total + n.
5: Return total.
Complexity Analysis
Proposition. The algorithm runs in time and space, where and .
Proof. The main loop iterates times. For each , computing requires at most division-and-lookup steps, each in . Total arithmetic operations: at most . Auxiliary space consists of the 10-entry factorial table and working variables.
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() {
int fact[10] = {1};
for (int i = 1; i <= 9; i++) fact[i] = fact[i - 1] * i;
int total = 0;
for (int n = 3; n <= 2540160; n++) {
int s = 0, tmp = n;
while (tmp > 0) { s += fact[tmp % 10]; tmp /= 10; }
if (s == n) total += n;
}
cout << total << endl; // 40730
return 0;
}
"""Project Euler Problem 34: Digit Factorials"""
def main():
fact = [1] * 10
for i in range(1, 10):
fact[i] = fact[i - 1] * i
total = 0
for n in range(3, 2540161):
s, tmp = 0, n
while tmp > 0:
s += fact[tmp % 10]
tmp //= 10
if s == n:
total += n
print(total) # 40730
if __name__ == "__main__":
main()