令人惊奇的,只有三个数能够写成它们各位数的四次方之和:
1634 = 1 4 + 6 4 + 3 4 + 4 4
8208 = 8 4 + 2 4 + 0 4 + 8 4
9474 = 9 4 + 4 4 + 7 4 + 4 4
1 = 14没有被算作在内因为它不是一个和。
这些数字的和是 1634 + 8208 + 9474 = 19316.
找出所有能被写成各位数字五次方之和的数之和。
public class Test {
public static int is_Answer(int n) {
int oldN = n;
int sum = 0;
int temp = 0;
while (n != 0) {
temp = n % 10;
sum += temp * temp * temp * temp * temp;
n /= 10;
}
if (oldN == sum)
return sum;
return 0;
}
public static void main(String[] args) {
int sum = 0;
for (int i = 10; i <= 354294; i++) {
sum += is_Answer(i);
}
System.out.println(sum);
}
}
对于算法的效率,参考了 http://blog.csdn.net/gaorongchao1990626/article/details/8296162