Pages

Sunday, September 13, 2009

Euler Problem 48 solution

Time (s): ~0.034
package margusmartseppcode.From_40_to_49;

public class Problem_48 {
 public static void main(String[] args) {
  long result = 0, d10 = 10000000000L;
  for (long u = 1, tmp = u; u <= 1000; result += tmp, ++u, tmp = u)
   for (long v = 1; v < u; ++v)
    tmp = (tmp * u) % d10;

  System.out.println(result % d10);
 }
}
Time (s): ~0.224
package margusmartseppcode.From_40_to_49;

import java.math.BigInteger;

public class Problem_48 {
 // Note: i = 2 !!!
 public static void main(String[] args) {
  BigInteger num = BigInteger.ONE;
  for (int i = 2; i < 1000; i++)
   num = num.add(BigInteger.valueOf(i).pow(i));
  String result = num.toString();
  System.out.println(result.substring(result.length() - 10));
 }
}

No comments:

Post a Comment