题目描述如下:
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
public class Problem34
{
public static void main(String[] args)
{
int sum = 0;
for (int i = 3; i < 1000000; i++)
{
if (isCurious(i))
{
System.out.println(i);
sum += i;
}
}
System.err.println("sum = " + sum);
}
public static boolean isCurious(int num)
{
int temp = num;
int sum = 0;
while (temp > 0)
{
sum += MathUtils.getFactorial(temp % 10);
temp = temp / 10;
}
if (sum == num)
{
return true;
}
return false;
}
}
运行结果:
145
40585
sum = 40730