project euler 30

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;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值