Project Euler Problem 34

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Project Euler Problem 34

Solution

Answer: 40730

We seek all integers $n$ such that

$$n = \sum (\text{factorial of each digit of } n).$$

For example,

$$145 = 1! + 4! + 5! = 1 + 24 + 120.$$

We must find the sum of all such numbers, excluding the trivial one-digit cases $1$ and $2$.


Mathematical analysis

Step 1: Precompute digit factorials

The factorials of the digits $0$ through $9$ are:

$$\begin{aligned} 0! &= 1\ 1! &= 1\ 2! &= 2\ 3! &= 6\ 4! &= 24\ 5! &= 120\ 6! &= 720\ 7! &= 5040\ 8! &= 40320\ 9! &= 362880 \end{aligned}$$

Since every number is built from decimal digits, any candidate number can be checked by summing these values.


Step 2: Find an upper bound

A brute-force search over all integers would be impossible unless we first establish a finite range.

Suppose a number has $d$ digits.

The largest possible sum of factorials of its digits occurs when every digit is $9$:

$$d \times 9!$$

because $9! = 362880$.

So any $d$-digit solution must satisfy

$$n \le d \cdot 9!$$

But a $d$-digit number is at least

$$10^{d-1}.$$

Therefore we require

$$10^{d-1} \le d \cdot 9!.$$

Now test values of $d$:

  • $7 \cdot 9! = 2540160$, which is only a 7-digit number.
  • $8 \cdot 9! = 2903040$, which is still only a 7-digit number.

But every 8-digit number is at least $10^7 = 10000000$.

Thus for $d \ge 8$,

$$d \cdot 9! < 10^{d-1},$$

so no 8-digit (or larger) solution exists.

Hence we only need to search up to

$$7 \cdot 9! = 2540160.$$

That makes brute force entirely feasible.


Step 3: Algorithm

For each integer $n$ from $10$ to $2540160$:

  1. Split $n$ into decimal digits.
  2. Replace each digit by its factorial.
  3. Sum these factorials.
  4. Check whether the sum equals $n$.

Collect all such numbers and sum them.

The known examples found are:

$$145,\quad 40585$$

and their sum is

$$145 + 40585 = 40730.$$


Python implementation

# Precompute factorials of digits 0-9
factorials = [1] * 10

for i in range(1, 10):
    factorials[i] = factorials[i - 1] * i

# Maximum possible candidate:
# 7 * 9! because larger digit counts cannot work
upper_bound = 7 * factorials[9]

total = 0
curious_numbers = []

# Start from 10 because 1 and 2 are excluded
for n in range(10, upper_bound + 1):

    # Sum factorials of digits
    digit_factorial_sum = sum(factorials[int(d)] for d in str(n))

    # Check if n satisfies the condition
    if digit_factorial_sum == n:
        curious_numbers.append(n)
        total += n

print("Curious numbers:", curious_numbers)
print("Sum:", total)

Code walkthrough

Precomputing factorials

factorials = [1] * 10

Creates a list of size 10 for storing $0!$ through $9!$.

for i in range(1, 10):
    factorials[i] = factorials[i - 1] * i

Builds factorial values iteratively:

$$1!, 2!, 3!, \dots, 9!$$

This avoids recomputing factorials repeatedly during the search.


Establishing the search range

upper_bound = 7 * factorials[9]

Uses the mathematical bound:

$$7 \cdot 9! = 2540160.$$

No larger number can possibly satisfy the condition.


Searching all candidates

for n in range(10, upper_bound + 1):

We start at 10 because the problem explicitly excludes $1$ and $2$.


Computing the factorial-digit sum

digit_factorial_sum = sum(factorials[int(d)] for d in str(n))

Example for $145$:

  • digits: $1,4,5$
  • factorials: $1,24,120$
  • sum: $145$

So the condition succeeds.


Recording valid numbers

if digit_factorial_sum == n:

If the equality holds, we store the number and add it to the running total.


Verification on small examples

For $145$:

$$1! + 4! + 5! = 1 + 24 + 120 = 145$$

For $40585$:

$$4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585$$

These are exactly the nontrivial solutions.

Their sum is:

$$145 + 40585 = 40730.$$


Final verification

  • The upper bound proof guarantees no solutions are missed.
  • The computation checks every candidate exhaustively.
  • Only two nontrivial numbers satisfy the condition.
  • Their sum is consistent with known Project Euler results.

Answer: 40730