Digit Fifth Powers
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
As is not a sum it is not included.
The sum of these numbers is .
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
Implementations
// 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_MODEint main(int argc, char const *argv[]) { std::cout << sum_digit_fifth_powers() << std::endl;}#endif // UNITTEST_MODESolution 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 (), calculating the fifth power sum of digits for each number and checking for equality. Time complexity is where is the upper bound and 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 can equal its digit fifth-power sum since 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.