Solution to Project Euler Problem 30: Digit fifth powers - Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: 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.
Updated: Aug. 10, 2022 — Training Time: 2 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 30: Digit fifth powers.
Difficulty: Easy.
Objective: Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 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.
Input: None.
Expected Output: 443839.
Sikademy Solution in Java Programming Language
package sikademy;
/**
*
* @author Archangel Macsika
* Copyright (c) Sikademy. All rights reserved
*/
public class SikademyEulerSolution {
public String run() {
int sum = 0;
for (int i = 2; i < 1000000; i++) {
if (i == fifthPowerDigitSum(i))
sum += i;
}
return Integer.toString(sum);
}
private static int fifthPowerDigitSum(int x) {
int sum = 0;
while (x != 0) {
int y = x % 10;
sum += y * y * y * y * y;
x /= 10;
}
return sum;
}
public static void main(String[] args) {
SikademyEulerSolution solution = new SikademyEulerSolution();
System.out.println(solution.run());
}
}
Sikademy Solution in Python Programming Language
#
# @author Archangel Macsika
# Copyright (c) Sikademy. All rights reserved.
#
def compute():
ans = sum(i for i in range(2, 1000000) if i == fifth_power_digit_sum(i))
return str(ans)
def fifth_power_digit_sum(n):
return sum(int(c)**5 for c in str(n))
if __name__ == "__main__":
print(compute())