Java solution to Project Euler Problem 48

Problem 48:

The series, 1^1 + 2^2 + 3^3 + … + 10^10 = 10405071317.

Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + … + 1000^1000.

Running time: 125 ms

Assessment: Again, very easy and fast using arbitrary-precision arithmetic. Like one of my other solutions, I didn’t limit the output to just the last ten digits in the series, but you could easily tack that on.

import java.math.BigInteger;
 
public class Problem048
{
	public static void main(String[] args)
	{
		long begin = System.currentTimeMillis();
		BigInteger sum = BigInteger.ZERO;
		BigInteger temp = BigInteger.ONE;
		BigInteger GrandTotal = BigInteger.ZERO;
 
		for (int i = 1; i <= 1000; i++)
		{
			sum = temp.pow(i);
			temp = temp.add(BigInteger.ONE);
			GrandTotal = GrandTotal.add(sum);
		}
 
		long end = System.currentTimeMillis();
 
		System.out.println(GrandTotal);
		System.out.println(end-begin + "ms");
	}
}

3 thoughts on “Java solution to Project Euler Problem 48

  1. Hi there. It’s all good and well… but this is certainly not the solution that the author had in mind. You solve it by brute force and it works only because you use arbitrary big numbers. Not every programming language has access to such an arithmetic, mind you. Where is exactly the place, for instance, that you use the information that 1^1 + 2^2 + 3^3 + … + 10^10 = 10405071317? I cannot see it. Therefore, in my opinion, this solution doesn’t count. Sorry.

  2. Your argument is that “not all programming languages have arbitrary precision arithmetic, therefore solutions that use arbitrary precision math libraries are invalid.”

    That’s ridiculous.

  3. copied your program exactly and got

    1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Leave a Reply

Your email address will not be published. Required fields are marked *