All Euler problems
Project Euler

Digit Factorial Chains

Define f(n) as the sum of factorials of the digits of n. Starting from any n, repeatedly applying f eventually enters a cycle. Let L(n) be the number of distinct values visited before entering a cy...

Source sync Apr 19, 2026
Problem #0933
Level Level 37
Solved By 167
Languages C++, Python
Answer 5707485980743099
Length 368 words
dynamic_programmingmodular_arithmeticsearch

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Starting with one piece of integer-sized rectangle paper, two players make moves in turn.

A valid move consists of choosing one piece of paper and cutting it both horizontally and vertically, so that it becomes four pieces of smaller rectangle papers, all of which are integer-sized.

The player that does not have a valid move loses the game.

Let $C(w, h)$ be the number of winning moves for the first player, when the original paper has size $w \times h$. For example, $C(5,3)=4$, with the four winning moves shown below.

Problem illustration

Also write $\displaystyle D(W, H) = \sum_{w = 2}^W\sum_{h = 2}^H C(w, h)$. You are given that $D(12, 123) = 327398$.

Find $D(123, 1234567)$.

Problem 933: Digit Factorial Chains

Mathematical Foundation

Definition. The digit factorial function is f(n)=ddigits(n)d!f(n) = \sum_{d \in \operatorname{digits}(n)} d!.

Theorem 1 (Orbit contraction). For all n107n \geq 10^7, we have f(n)<nf(n) < n. Consequently, every orbit of ff eventually enters the interval [1,2540160][1, 2540160].

Proof. A kk-digit number nn satisfies n10k1n \geq 10^{k-1}. Since each digit is at most 9, we have f(n)k9!=362880kf(n) \leq k \cdot 9! = 362880k. For k8k \geq 8: 362880k362880k<10k1362880k \leq 362880 \cdot k < 10^{k-1} (verified: 3628808=2903040<107362880 \cdot 8 = 2903040 < 10^7). Hence f(n)<nf(n) < n for all n107n \geq 10^7.

For k=7k = 7: f(n)7362880=2540160f(n) \leq 7 \cdot 362880 = 2540160. Since every orbit eventually reaches a value 2540160\leq 2540160, and 2540160<1072540160 < 10^7, all subsequent iterates remain below 10710^7. Within [1,2540160][1, 2540160], the orbit must eventually revisit a value (by the pigeonhole principle), entering a cycle. \square

Theorem 2 (Complete cycle classification). The digit factorial function ff in base 10 has exactly the following attractors:

  • Fixed points: 11, 22, 145145, 4058540585.
  • 2-cycles: {871,45361}\{871, 45361\} and {872,45362}\{872, 45362\}.
  • 3-cycle: {169,363601,1454}\{169, 363601, 1454\}.

Proof. This is verified by exhaustive computation: iterate ff from every starting value in [1,2540160][1, 2540160] and record the terminal cycle. By Theorem 1, every orbit enters this range, so no cycle outside it exists. The listed cycles are confirmed by direct evaluation:

  • f(1)=1f(1) = 1, f(2)=2f(2) = 2, f(145)=1!+4!+5!=145f(145) = 1! + 4! + 5! = 145, f(40585)=4!+0!+5!+8!+5!=40585f(40585) = 4! + 0! + 5! + 8! + 5! = 40585.
  • f(871)=8!+7!+1!=45361f(871) = 8! + 7! + 1! = 45361, f(45361)=4!+5!+3!+6!+1!=871f(45361) = 4! + 5! + 3! + 6! + 1! = 871.
  • f(169)=1!+6!+9!=363601f(169) = 1! + 6! + 9! = 363601, f(363601)=1454f(363601) = 1454, f(1454)=169f(1454) = 169. \square

Lemma 1 (Memoization correctness). If we store L(m)L(m) for each visited value mm, then for any nn whose orbit passes through mm, we can compute L(n)=(steps from n to m)+L(m)L(n) = (\text{steps from } n \text{ to } m) + L(m).

Proof. The orbit is deterministic (ff is a function), so the sequence of values from nn is uniquely determined. If mm is the first value in the orbit of nn for which L(m)L(m) is already known, then the total distinct values from nn equal the new values before reaching mm plus the distinct values from mm onward. \square

Editorial

Define f(n) = sum of factorials of digits of n. Starting from any n, repeatedly apply f to form a chain until a value repeats. Let L(n) be the number of distinct values in this chain. Compute sum_{n=1}^{N} L(n). Key observations:. We precompute factorials. We then f(n): sum of digit factorials. Finally, mark cycle members with their cycle contribution.

Pseudocode

Precompute factorials
f(n): sum of digit factorials
Memoization table: L[m] = chain length from m
Mark cycle members with their cycle contribution
(Fixed points have L = 1; 2-cycle members have L = 2; 3-cycle members have L = 3)
for n from 1 to N
if n in L
Follow orbit, recording path
while current not in L and current not in path
if current in L
Backfill from end of path
else
current is in path: found a new cycle

Complexity Analysis

  • Time: O(NC)O(N \cdot C) where CC is the average chain length before hitting a cached value. Since orbits contract rapidly (Theorem 1), CC is bounded by a constant (at most 60\sim 60 steps to enter a cycle from any starting point below 10610^6). With memoization, amortized cost is O(N)O(N).
  • Space: O(N+B)O(N + B) where B=2540160B = 2540160 is the bound from Theorem 1. In practice, O(N)O(N) for the memoization table.

Answer

5707485980743099\boxed{5707485980743099}

Code

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

C++ project_euler/problem_933/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int FACT[]={1,1,2,6,24,120,720,5040,40320,362880};
int dfs(int n){int s=0;while(n){s+=FACT[n%10];n/=10;}return s;}
int main(){
    const int N=1000000;
    vector<int> L(N+1,0);
    for(int n=1;n<=N;n++){
        unordered_set<int> seen;
        int cur=n;
        while(!seen.count(cur)){seen.insert(cur);cur=dfs(cur);}
        L[n]=seen.size();
    }
    long long ans=0;
    for(int i=1;i<=N;i++) ans+=L[i];
    cout<<ans<<endl;
    return 0;
}