Pages

Sunday, September 13, 2009

Euler Problem 48 solution

Time (s): ~0.034
  1. package margusmartseppcode.From_40_to_49;  
  2.   
  3. public class Problem_48 {  
  4.  public static void main(String[] args) {  
  5.   long result = 0, d10 = 10000000000L;  
  6.   for (long u = 1, tmp = u; u <= 1000; result += tmp, ++u, tmp = u)  
  7.    for (long v = 1; v < u; ++v)  
  8.     tmp = (tmp * u) % d10;  
  9.   
  10.   System.out.println(result % d10);  
  11.  }  
  12. }  
Time (s): ~0.224
  1. package margusmartseppcode.From_40_to_49;  
  2.   
  3. import java.math.BigInteger;  
  4.   
  5. public class Problem_48 {  
  6.  // Note: i = 2 !!!  
  7.  public static void main(String[] args) {  
  8.   BigInteger num = BigInteger.ONE;  
  9.   for (int i = 2; i < 1000; i++)  
  10.    num = num.add(BigInteger.valueOf(i).pow(i));  
  11.   String result = num.toString();  
  12.   System.out.println(result.substring(result.length() - 10));  
  13.  }  
  14. }  

No comments:

Post a Comment