Project Euler NO30

令人驚奇的,只有三個數能夠寫成它們各位數的四次方之和:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
1 = 14沒有被算作在內因爲它不是一個和。
這些數字的和是 1634 + 8208 + 9474 = 19316.

找出所有能被寫成各位數字五次方之和的數之和。



從10開始到 5個9^5結束~~

public class Problem30
{
	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 sum = 0;
		int max = 0;
		for (int i = 0; i < 5; i++)
		{
			max += Math.pow(9, 5);
		}
		for (int i = 10; i <= max; i++)
		{
			int s = 0;
			int t = i;
			while (t != 0)
			{
				s += Math.pow(t % 10, 5);
				t /= 10;
			}
			
			if (s == i)
			{
				sum += i;
			}
		}
		
		System.out.println(sum);
	}
}



answer:  443839
time:  352


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章