欧拉计划34题

/**
* Digit factorials
* Problem 34
*
* 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.
*
* @author dcb
*
*/
求所有各个位数阶乘和等于本身的数的和

分析:
题目不能,使用循环校验就行,难点在于划分范围
一个10进制数每个位上的最大值为9
而 9!= 362880 共计6位数
9999999 各个位数上的阶乘和位 362880*7=2540160
因此只需循环至2540160就行

代码实现
int result = 0;
for (int i = 10; i < 2540160; i++){
String num = "" + i;
int temp = 0;
for (int j = 0; j < num.length(); j++){
int a = new Integer("" + num.charAt(j));
temp += Utils.factorial(a);
}
if (i == temp){
System.out.println(i);
result += i;
}
}


结果:40730
令人惊讶的是
满足条件的数字只有两个
145
40585









猜你喜欢

转载自535260620.iteye.com/blog/2283693