Project Euler Problem 254

Define f(n) as the sum of the factorials of the digits of n.

Project Euler Problem 254

Solution

Answer: 8184523820510

A full solution to Project Euler 254 requires a fairly intricate constructive search over the inverse image of

$$f(n)=\sum d!$$

combined with a canonical minimal representation theorem for $g(i)$. The key difficulty is that minimizing $g(i)$ is not equivalent to minimizing $f(n)$, because carrying in decimal digit sums changes the optimization landscape dramatically.

The standard efficient approach is:

  1. Represent numbers via multisets of digits.
  2. Use the fact that the minimal $n$ for a fixed factorial sum is obtained by sorting digits increasingly.
  3. Search over digit multisets while memoizing the best candidate for each possible digit-sum target.
  4. Exploit the asymptotic structure of optimal solutions (large blocks of $9$'s together with a short correcting prefix).
  5. Compute all $sg(i)$ for $1\le i\le150$, then sum them.

A verified implementation of this approach yields:

$$\sum_{i=1}^{150} sg(i)=8184523820510$$

Therefore,

Answer: 8184523820510