Tim Varley Logo
Tim Varley Systems Engineer
Problem #30 hard

Digit Fifth Powers

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634=14+64+34+448208=84+24+04+849474=94+44+74+44\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=141 = 1^4 is not a sum it is not included.

The sum of these numbers is 1634+8208+9474=193161634 + 8208 + 9474 = 19316.

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

Implementations

cpp
// https://projecteuler.net/problem=30
// Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
//
// 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
//
// 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.
//
// Answer: 443839
// Authored by: Tim Varley 💘
// Assisted-by: Grok Code Fast via Crush 💘 <crush@charm.land>
#include <iostream>
#include <vector>
int sum_of_fifth_powers_of_digits(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
int power = 1;
for (int i = 0; i < 5; ++i) power *= digit;
sum += power;
n /= 10;
}
return sum;
}
int sum_digit_fifth_powers() {
int total = 0;
// Upper bound: 6 * 9^5 = 354294
for (int i = 2; i <= 354294; ++i) {
if (sum_of_fifth_powers_of_digits(i) == i) {
total += i;
}
}
return total;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << sum_digit_fifth_powers() << std::endl;
}
#endif // UNITTEST_MODE

Solution Notes

Mathematical Background

This problem explores narcissistic numbers, specifically those equal to the sum of their digits raised to the fifth power. The fourth-power case demonstrates that such numbers exist and are rare, while the fifth-power variant requires finding all such numbers and summing them.

Algorithm Analysis

The solution iterates through all numbers up to an upper bound determined by the maximum possible digit sum (6×95=3542946 \times 9^5 = 354294), calculating the fifth power sum of digits for each number and checking for equality. Time complexity is O(n×d)O(n \times d) where nn is the upper bound and dd is the average number of digits (constant), making it effectively linear in the search space.

Key Insights

The upper bound is crucial: no number larger than 6×956 \times 9^5 can equal its digit fifth-power sum since 7×95=4133437 \times 9^5 = 413343 is a 6-digit number while the maximum 6-digit number is 999999. The solution finds six such numbers whose fifth powers sum to 443839.

Educational Value

This problem teaches the importance of establishing bounds in computational searches and demonstrates how mathematical constraints can dramatically reduce the search space. It combines digit manipulation, exponentiation, and summation in a clear algorithmic framework, illustrating efficient brute-force approaches to number theory problems.