Java Implementation of Problem 34
View source code here on GitHub!
1/*
2Project Euler Problem 34
3
4This ended up being a filtering problem. The problem with my solution is that I
5am not satisfied with my filter at all. I feel like there is a more efficient
6way to go about it.
7
8Problem:
9
10145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
11
12Find the sum of all numbers which are equal to the sum of the factorial of
13their digits.
14
15Note: as 1! = 1 and 2! = 2 are not sums they are not included.
16*/
17package euler;
18
19import euler.lib.Mathematics;
20
21public class p0034 implements IEuler {
22 @Override
23 public Object answer() {
24 int answer = 0;
25 for (int x = 10; x < 100000; x++) {
26 String xs = Integer.toString(x);
27 int sum = 0;
28 for (byte i = 0; i < xs.length(); i++)
29 sum += (int) Mathematics.factorial(xs.charAt(i) - '0');
30 if (sum == x)
31 answer += x;
32 }
33 return (short) answer;
34 }
35}