All Euler problems
Project Euler

Digit Power Sum Sequences

Define f(n) = sum_(d in digits(n)) d^5. Starting from any n, the sequence n, f(n), f(f(n)),... eventually enters a cycle. Find the sum of all starting values n <= 10^6 that eventually reach 1.

Source sync Apr 19, 2026
Problem #0915
Level Level 34
Solved By 223
Languages C++, Python
Answer 55601924
Length 474 words
geometrydynamic_programmingmodular_arithmetic

Problem Statement

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

The function \(s(n)\) is defined recursively for positive integers by \(s(1) = 1\) and \(s(n+1) = \big (s(n) - 1\big )^3 +2\) for \(n\geq 1\).

The sequence begins: \(s(1) = 1, s(2) = 2, s(3) = 3, s(4) = 10, \ldots \).

For positive integers \(N\), define \[T(N) = \sum _{a=1}^N \sum _{b=1}^N \gcd \Big (s\big (s(a)\big ), s\big (s(b)\big )\Big ).\] You are given \(T(3) = 12\), \(T(4) \equiv 24881925\) and \(T(100)\equiv 14416749\) both modulo \(123456789\).

Find \(T(10^8)\). Give your answer modulo \(123456789\).

Problem 915: Digit Power Sum Sequences

Mathematical Analysis

Orbit Contraction

Theorem. For any n107n \geq 10^7 (7+ digits), f(n)<nf(n) < n. Hence every orbit under ff eventually enters the range [1,413343][1, 413343] and must cycle.

Proof. A dd-digit number nn satisfies n10d1n \geq 10^{d-1}. The maximum value of ff on dd-digit numbers is d95=59049dd \cdot 9^5 = 59049d. For d7d \geq 7:

590497=413343<106<10d1n59049 \cdot 7 = 413343 < 10^6 < 10^{d-1} \leq n

so f(n)<nf(n) < n. The orbit is strictly decreasing until it enters [1,413343][1, 413343], where it must revisit some value (pigeonhole), forming a cycle. \square

Fixed Points of ff

Lemma. n=1n = 1 is a fixed point of ff: f(1)=15=1f(1) = 1^5 = 1.

There are very few fixed points. For nn to be a fixed point, n=di5n = \sum d_i^5 where did_i are the digits of nn. Searching up to 413343413343:

  • f(1)=1f(1) = 1 (fixed point)
  • Other possible fixed points and cycles can be found by iterating ff on all values up to 413343413343.

Classification of Eventual Behavior

Every positive integer nn eventually enters one of finitely many cycles under ff. The cycles for p=5p = 5 (fifth-power digit sum) include:

  • Fixed point at 1: 111 \to 1
  • Various longer cycles (e.g., involving numbers like 4150, 4151, etc.)

The Armstrong numbers (narcissistic numbers) for power 5 are: 1, 4150, 4151, 54748, 92727, 93084, 194979. These satisfy n=f(n)n = f(n).

Memoized Orbit Classification

Algorithm. For each nn from 1 to 10610^6:

  1. Follow the orbit n,f(n),f(f(n)),n, f(n), f(f(n)), \ldots until revisiting a known value.
  2. If the orbit reaches a value already classified as “reaches 1” or “does not reach 1,” inherit that classification.
  3. Cache the result for all values along the current path.

Theorem. This algorithm runs in O(N)O(N) total time with O(N)O(N) space, because each value is visited at most twice (once in its own orbit computation, once when another orbit passes through it).

Counting the Happy Numbers (Fifth-Power Variant)

Definition. A number nn is 5-happy if its orbit under ff eventually reaches 1.

For the classical happy-number problem (power 2), roughly 14.3% of numbers are happy. For power 5, the density differs.

Concrete Examples

nnOrbitReaches 1?
1111 \to 1Yes
2232275179752 \to 32 \to 275 \to 17975 \to \ldotsCheck
1010110 \to 1Yes
1001001100 \to 1Yes
999999395=177147999 \to 3 \cdot 9^5 = 177147 \to \ldotsCheck
4150415045+1+55+0=1024+1+3125=41504150 \to 4^5+1+5^5+0 = 1024+1+3125 = 4150No (cycle at self)

Upper Bound on Orbit Length

Lemma. For any n106n \leq 10^6, the orbit reaches a value 413343\leq 413343 within at most 2 applications of ff. From there, the orbit length before cycling is bounded by 413343413343 (in the worst case, but typically much less).

Proof. f(n)695=354294f(n) \leq 6 \cdot 9^5 = 354294 for any 6-digit nn. And f(m)695=354294f(m) \leq 6 \cdot 9^5 = 354294 for any m413343m \leq 413343. So after one step, we are in [1,354294][1, 354294] and stay there. \square

Proof of Correctness

The memoization algorithm correctly identifies whether each orbit reaches 1:

  1. Termination: Guaranteed because orbits contract into a finite range and must cycle.
  2. Correctness of caching: Once a value’s classification is determined, it is permanent---the orbit is deterministic.
  3. Completeness: Every n106n \leq 10^6 is checked.

Complexity Analysis

  • Time: O(N)O(N) amortized---each number is processed once.
  • Space: O(N)O(N) for the memoization table.
  • Per-step cost: O(logn)O(\log n) to compute digit sum (at most 6 digits for n106n \leq 10^6).

Answer

55601924\boxed{55601924}

Code

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

C++ project_euler/problem_915/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 915: Digit Power Sum Sequences
 *
 * f(n) = sum of 5th powers of digits of n.
 * Sum all n <= 10^6 whose orbit eventually reaches 1.
 *
 * Key observations:
 *   - f(n) < n for n >= 10^7, so orbits contract into [1, 413343].
 *   - Memoize orbit classification: reaches 1 or not.
 *   - Armstrong numbers for p=5: 1, 4150, 4151, 54748, 92727, 93084, 194979.
 *   - 1 is the only fixed point that qualifies.
 *
 * Complexity: O(N) amortized time with O(N) space.
 */

static const int N = 1000000;
static const int CACHE_SIZE = 500000;
static const int POW5[] = {0, 1, 32, 243, 1024, 3125, 7776, 16807, 32768, 59049};

int digit_pow5(int n) {
    int s = 0;
    while (n > 0) {
        s += POW5[n % 10];
        n /= 10;
    }
    return s;
}

// 0 = unknown, 1 = reaches 1, 2 = does not reach 1
int cache[CACHE_SIZE];

bool reaches_one(int n) {
    if (n < CACHE_SIZE && cache[n]) return cache[n] == 1;

    vector<int> path;
    unordered_set<int> visited;
    int x = n;

    while (visited.find(x) == visited.end() && (x >= CACHE_SIZE || !cache[x])) {
        visited.insert(x);
        path.push_back(x);
        x = digit_pow5(x);
    }

    bool result;
    if (x < CACHE_SIZE && cache[x]) {
        result = (cache[x] == 1);
    } else if (x == 1) {
        result = true;
    } else {
        // Found a cycle; check if 1 is in it
        result = false;
        int y = x;
        do {
            if (y == 1) { result = true; break; }
            y = digit_pow5(y);
        } while (y != x);
    }

    for (int v : path) {
        if (v < CACHE_SIZE) {
            cache[v] = result ? 1 : 2;
        }
    }
    return result;
}

int main() {
    memset(cache, 0, sizeof(cache));
    cache[1] = 1; // 1 is a fixed point: f(1) = 1

    long long total = 0;
    for (int n = 1; n <= N; n++) {
        if (reaches_one(n)) {
            total += n;
        }
    }

    cout << total << endl;

    // Verification: Armstrong numbers for p=5
    // These are fixed points of f: n = f(n)
    vector<int> armstrong = {1, 4150, 4151, 54748, 92727, 93084, 194979};
    for (int a : armstrong) {
        assert(digit_pow5(a) == a);
    }

    return 0;
}