All Euler problems
Project Euler

Digit Fifth Powers

Find the sum of all numbers that can be written as the sum of fifth powers of their digits. (By convention, 1 = 1^5 is excluded since it is not a "sum.")

Source sync Apr 19, 2026
Problem #0030
Level Level 01
Solved By 120,528
Languages C++, Python
Answer 443839
Length 437 words
searchmodular_arithmeticbrute_force

Problem Statement

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

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: \begin {align*} 1634 &= 1^4 + 6^4 + 3^4 + 4^4\\ 8208 &= 8^4 + 2^4 + 0^4 + 8^4\\ 9474 &= 9^4 + 4^4 + 7^4 + 4^4 \end {align*}

As \(1 = 1^4\) is not a sum it is not included.

The sum of these numbers is \(1634 + 8208 + 9474 = 19316\).

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

Problem 30: Digit Fifth Powers

Mathematical Development

Definition 1. For a positive integer nn with decimal digits d1,d2,,dmd_1, d_2, \ldots, d_m, define the digit power sum of order kk as σk(n)=i=1mdik\sigma_k(n) = \sum_{i=1}^{m} d_i^k. A number n2n \geq 2 is a narcissistic number of order kk (also called a pluperfect digital invariant) if σk(n)=n\sigma_k(n) = n.

Theorem 1 (Finiteness of Search Space). For any fixed k2k \geq 2, the set {n2:σk(n)=n}\{n \geq 2 : \sigma_k(n) = n\} is finite. In particular, every such nn satisfies nUkn \leq U_k, where UkU_k is computable.

Proof. A dd-digit number satisfies n10d1n \geq 10^{d-1}. The maximum value of σk(n)\sigma_k(n) for a dd-digit number is d9kd \cdot 9^k, since each digit is at most 9. For σk(n)=n\sigma_k(n) = n to hold, we need d9k10d1d \cdot 9^k \geq 10^{d-1}. Since 10d110^{d-1} grows exponentially in dd while d9kd \cdot 9^k grows linearly, there exists a threshold DkD_k beyond which d9k<10d1d \cdot 9^k < 10^{d-1}, making the equation σk(n)=n\sigma_k(n) = n impossible. Therefore nn has at most DkD_k digits, and nDk9kn \leq D_k \cdot 9^k. \square

Lemma 1 (Upper Bound for k=5k = 5). If σ5(n)=n\sigma_5(n) = n, then n354294n \leq 354294.

Proof. We have 95=590499^5 = 59049. For a dd-digit number, σ5(n)d59049\sigma_5(n) \leq d \cdot 59049. We need d5904910d1d \cdot 59049 \geq 10^{d-1}:

ddd59049d \cdot 5904910d110^{d-1}Feasible
6354,294100,000Yes
7413,3431,000,000No

For d7d \geq 7, d59049<10d1d \cdot 59049 < 10^{d-1}, so no dd-digit number with d7d \geq 7 can be narcissistic of order 5. Hence nn has at most 6 digits and n695=354294n \leq 6 \cdot 9^5 = 354294. \square

Theorem 2 (Completeness of Exhaustive Search). The exhaustive search over {2,3,,354294}\{2, 3, \ldots, 354294\} finds all narcissistic numbers of order 5.

Proof. By Lemma 1, every narcissistic number of order 5 lies in [2,354294][2, 354294]. For each nn in this range, we compute σ5(n)\sigma_5(n) directly and check whether σ5(n)=n\sigma_5(n) = n. Since the search range contains all candidates, the enumeration is complete. \square

Theorem 3 (Enumeration of Solutions). The narcissistic numbers of order 55 (with n2n \geq 2) are exactly:

nnVerification
415045+15+55+05=1024+1+3125+0=41504^5 + 1^5 + 5^5 + 0^5 = 1024 + 1 + 3125 + 0 = 4150
415145+15+55+15=1024+1+3125+1=41514^5 + 1^5 + 5^5 + 1^5 = 1024 + 1 + 3125 + 1 = 4151
5474855+45+75+45+85=3125+1024+16807+1024+32768=547485^5 + 4^5 + 7^5 + 4^5 + 8^5 = 3125 + 1024 + 16807 + 1024 + 32768 = 54748
9272795+25+75+25+75=59049+32+16807+32+16807=927279^5 + 2^5 + 7^5 + 2^5 + 7^5 = 59049 + 32 + 16807 + 32 + 16807 = 92727
9308495+35+05+85+45=59049+243+0+32768+1024=930849^5 + 3^5 + 0^5 + 8^5 + 4^5 = 59049 + 243 + 0 + 32768 + 1024 = 93084
19497915+95+45+95+75+95=1+59049+1024+59049+16807+59049=1949791^5 + 9^5 + 4^5 + 9^5 + 7^5 + 9^5 = 1 + 59049 + 1024 + 59049 + 16807 + 59049 = 194979

Proof. By exhaustive search over [2,354294][2, 354294], these are the only nn satisfying σ5(n)=n\sigma_5(n) = n. \square

Corollary (Answer). The sum of all narcissistic numbers of order 5 is

4150+4151+54748+92727+93084+194979=443839.4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839.

Editorial

We perform a bounded exhaustive search. The algorithm precomputes the fifth power of each digit, iterates through every candidate from 2 up to the proven upper bound 6956 \cdot 9^5, computes the sum of the fifth powers of its digits, and adds the number when the digit-power sum matches the original value. This is sufficient because Lemma 1 shows that no solution can lie above that bound.

Pseudocode

Algorithm: Sum of Numbers Equal to the Sum of Fifth Powers of Their Digits
Require: The decimal digit set {0, 1, ..., 9}.
Ensure: The sum of all integers that equal the sum of the fifth powers of their decimal digits.
1: Precompute w(d) ← d^5 for each digit d.
2: Initialize total ← 0.
3: For each candidate n in {2, 3, ..., 6 · 9^5}, compute s ← sum of w(d) over the decimal digits d of n.
4: If s = n, update total ← total + n.
5: Return total.

Complexity Analysis

Proposition (Time Complexity). The algorithm runs in O(Ulog10U)O(U \cdot \log_{10} U) time where U=354294U = 354294.

Proof. The outer loop iterates U1U - 1 times. For each nn, extracting digits and summing their fifth powers takes log10n+16\lfloor \log_{10} n \rfloor + 1 \leq 6 iterations. The fifth-power lookup is O(1)O(1) via the precomputed table. Total: O(U6)=O(U)O(U \cdot 6) = O(U). More precisely, Ulog10U354294×62.1×106U \cdot \lceil \log_{10} U \rceil \approx 354294 \times 6 \approx 2.1 \times 10^6 operations. \square

Proposition (Space Complexity). The algorithm uses O(1)O(1) auxiliary space (the precomputed table has fixed size 10).

Answer

443839\boxed{443839}

Code

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

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

int main() {
    int pow5[10];
    for (int i = 0; i < 10; i++)
        pow5[i] = i * i * i * i * i;

    int total = 0;
    for (int n = 2; n <= 354294; n++) {
        int s = 0, t = n;
        while (t > 0) {
            s += pow5[t % 10];
            t /= 10;
        }
        if (s == n)
            total += n;
    }

    cout << total << endl;
    return 0;
}