Project Euler NO48

11 + 22 + 33 + ... + 1010 = 10405071317.

11 + 22 + 33 + ... + 10001000的最後十位是什麼?




java  BigInteger


import java.math.BigInteger;


public class Problem48
{
	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()
	{
		BigInteger sum = BigInteger.ZERO;
		for (int i = 1; i <= 1000; i++)
		{
			sum = sum.add(BigInteger.valueOf(i).pow(i));
		}
		
		String s= sum.toString();
		System.out.println(s.substring(s.length() - 10));
	}
}

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