Problem 30
Digit fifth powers
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
各位数字的五次幂
令人惊讶的是,只有三个数可以写成它们各位数字的四次幂之和:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
由于1 = 14不是一个和,所以这里并没有把它包括进去。
这些数的和是1634 + 8208 + 9474 = 19316。
找出所有可以写成它们各位数字的五次幂之和的数,并求这些数的和。
package projecteuler;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class Prj30 {
/**
* Surprisingly there are only three numbers that can be written as the sum
* of fourth powers of their digits:
*
* 1634 = 14 + 64 + 34 + 44 8208 = 84 + 24 + 04 + 84 9474 = 94 + 44 + 74 +
* 44 As 1 = 14 is not a sum it is not included.
*
* The sum of these numbers is 1634 + 8208 + 9474 = 19316.
*
* Find the sum of all the numbers that can be written as the sum of fifth
* powers of their digits.
*/
@Test
public void test() {
int sum = 0;
for (int i = 10; i < Math.pow(10, 6); i++) {
if (checkCondition(i)) {
System.out.println(i);
sum += i;
}
}
System.out.println("sum=" + sum);
}
private boolean checkCondition(int val) {
List<Integer> data = new ArrayList<Integer>();
int num = val;
while( num >= 10){
data.add(num % 10);
num /= 10;
}
data.add(num);
int sum = 0;
for( int i = 0 ; i < data.size(); i ++){
sum += Math.pow(data.get(i), 5);
}
return sum == val;
}
public static class CalculateParams{
/**
* 横向网格点数
*/
public int m;
/**
* 纵向网格个数
*/
public int n;
/**
* 网格间距
*/
public int netSize;
/**
* 网格点数值
*/
public double[] val;
/**
* 等直面数值数组
*/
public double[] contourVal;
/**
* 等直面个数
*/
public int coutourNum;
}
}