Problem:
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.
Solution:
443839
Code:
The solution may include methods that will be found here: Library.java .
public interface EulerSolution{ public String run(); }
/* * Solution to Project Euler problem 30 * By Nayuki Minase * * http://nayuki.eigenstate.org/page/project-euler-solutions * https://github.com/nayuki/Project-Euler-solutions */ public final class p030 implements EulerSolution { public static void main(String[] args) { System.out.println(new p030().run()); } public String run() { // As stated in the problem, 1 = 1^5 is excluded. // If a number has at least n >= 7 digits, then even if every digit is 9, // n * 9^5 is still less than the number (which is at least 10^n). 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; } }
No comments :
Post a Comment