Problem specification: ProjectEuler.net Problem 48
"Find the last ten digits of the series, 1^(1) + 2^(2) + 3^(3) + ... + 1000^(1000)" - with Java, we require "BigInteger" class. Please note that the creation of a new BigInteger object takes a string. Hence the code of 'new BigInteger(""+i)'
Execution time: ~100ms (laptop)
CODE
package projecteuler;
import java.math.BigInteger;
/**
* Java solution to project euler problem 48
* @author Mike
*/
public class Problem_48 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
BigInteger n = BigInteger.valueOf(0);
for(int i = 1; i <= 1000; i++){
n = n.add(new BigInteger(""+i).pow(i));
}
System.out.println(n.toString().substring(n.toString().length() - 10));
long stop = System.currentTimeMillis();
System.out.println("Time: " + (stop-start) + "ms");
}
}
Any suggestions and comments are welcome, please send me an email (see contacts).