All Euler problems
Project Euler

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...

Source sync Apr 19, 2026
Problem #0034
Level Level 01
Solved By 103,751
Languages C++, Python
Answer 40730
Length 384 words
modular_arithmeticsearchbrute_force

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.

Note:As \(1! = 1\) and \(2! = 2\) are not sums they are not included.

Problem 34: Digit Factorials

Mathematical Development

Definition 1. For a positive integer nn with decimal representation d1d2dkd_1 d_2 \ldots d_k (where d10d_1 \ne 0), define the digit factorial sum

S(n)=i=1kdi!S(n) = \sum_{i=1}^{k} d_i!

A positive integer n3n \ge 3 is called a factorion if S(n)=nS(n) = n.

Theorem 1 (Upper bound on factorions). If nn is a factorion, then n2,540,160n \le 2{,}540{,}160. Equivalently, every factorion has at most 77 digits.

Proof. Let nn have kk digits. Then n10k1n \ge 10^{k-1} (for k2k \ge 2) and S(n)k9!=362,880kS(n) \le k \cdot 9! = 362{,}880k. A necessary condition for S(n)=nS(n) = n is 362,880k10k1362{,}880k \ge 10^{k-1}, i.e.,

g(k):=10k1362,880k1.g(k) := \frac{10^{k-1}}{362{,}880k} \le 1.

We evaluate g(k)g(k) for critical values:

kk10k110^{k-1}362,880k362{,}880kg(k)g(k)
71,000,0001{,}000{,}0002,540,1602{,}540{,}1600.39410.394 \le 1
810,000,00010{,}000{,}0002,903,0402{,}903{,}0403.44>13.44 > 1

Claim. g(k)>1g(k) > 1 for all k8k \ge 8.

Proof of claim. We proceed by induction. Base case: g(8)=107/(362,8808)3.44>1g(8) = 10^7 / (362{,}880 \cdot 8) \approx 3.44 > 1. Inductive step: assume g(k)>1g(k) > 1 for some k8k \ge 8. Then

g(k+1)=10k362,880(k+1)=1010k1362,880(k+1)=g(k)10kk+1.g(k+1) = \frac{10^k}{362{,}880(k+1)} = \frac{10 \cdot 10^{k-1}}{362{,}880(k+1)} = g(k) \cdot \frac{10k}{k+1}.

For k8k \ge 8, 10kk+1=1010k+110109>8>1\frac{10k}{k+1} = 10 - \frac{10}{k+1} \ge 10 - \frac{10}{9} > 8 > 1, so g(k+1)>g(k)>1g(k+1) > g(k) > 1.

Therefore no kk-digit number with k8k \ge 8 can be a factorion. Since S(n)79!=2,540,160S(n) \le 7 \cdot 9! = 2{,}540{,}160 for any 7-digit number, the search space is {3,4,,2,540,160}\{3, 4, \ldots, 2{,}540{,}160\}. \square

Lemma 1 (Precomputation). The digit factorials are:

dd0123456789
d!d!112624120720504040320362880

Using this lookup table, S(n)S(n) can be computed in O(log10n+1)O(\lfloor \log_{10} n \rfloor + 1) time.

Proof. Extract digits by repeated division by 10, summing the looked-up factorials. The number of divisions equals the number of digits. \square

Theorem 2 (Complete classification). The only factorions with n3n \ge 3 are n=145n = 145 and n=40,585n = 40{,}585.

Proof. By Theorem 1, it suffices to evaluate S(n)S(n) for all n{3,,2,540,160}n \in \{3, \ldots, 2{,}540{,}160\} and check S(n)=nS(n) = n. This exhaustive computation identifies exactly two solutions:

  • S(145)=1!+4!+5!=1+24+120=145S(145) = 1! + 4! + 5! = 1 + 24 + 120 = 145.
  • S(40,585)=4!+0!+5!+8!+5!=24+1+120+40,320+120=40,585S(40{,}585) = 4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40{,}320 + 120 = 40{,}585.

No other value in the search range satisfies the factorion condition. \square

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 79!7 \cdot 9! 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 O(Udmax)O(U \cdot d_{\max}) time and O(1)O(1) space, where U=2,540,160U = 2{,}540{,}160 and dmax=7d_{\max} = 7.

Proof. The main loop iterates U2U - 2 times. For each nn, computing S(n)S(n) requires at most dmax=7d_{\max} = 7 division-and-lookup steps, each in O(1)O(1). Total arithmetic operations: at most 7(U2)1.78×1077(U - 2) \approx 1.78 \times 10^7. Auxiliary space consists of the 10-entry factorial table and O(1)O(1) working variables. \square

Answer

40730\boxed{40730}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_034/solution.cpp
#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;
}