145 是一个奇怪的数字, 因为 1! + 4! + 5! = 1 + 24 + 120 = 145.
找出所有等于各位数字阶乘之和的数字之和。
找出所有等于各位数字阶乘之和的数字之和。
注意: 因为 1! = 1 和 2! = 2 不是和的形式,所以它们不算在内。
import java.util.Arrays;
public class Problem34
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
System.out.print("answer: ");
howmany();
long end = System.currentTimeMillis();
System.out.print("time: ");
System.out.println(end - start);
}
static void howmany()
{
int total = 0;
int array[] = new int[10];//用数组存下 0 ~ 9 的阶乘加快计算
for (int i = 0; i <= 9; i++)
{
array[i] = jiecheng(i);
}
for (int i = 11; i <= 2540160; i++)// 9! * 7 为最大
{
int t = i;
int sum = 0;
while (t != 0)
{
sum += array[t % 10];
t /= 10;
}
if (sum == i)
{
total += i;
}
}
System.out.println(total);
}
static int jiecheng(int n)
{
int sum = 1;
for (int i = 2; i <= n; i++)
{
sum *= i;
}
return sum;
}
}
answer: 40730
time: 90