lordmat

Problem 48

Jun 14th, 2011
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Mat
  4.  */
  5. package problem.pkg48;
  6.  
  7. import java.math.BigInteger;
  8.  
  9. public class Problem48 {
  10.  
  11.  
  12.     public static void main(String[] args) {
  13.         BigInteger total = new BigInteger("1"); // first result 1^1 = 1
  14.        
  15.         // loop through starting at 2^2 + then 3^3 +... 1000^1000 into total
  16.         for(BigInteger i = BigInteger.valueOf(2);
  17.                 i.compareTo(BigInteger.valueOf(1000)) <= 0 ; i = i.add(BigInteger.ONE)){
  18.             total = total.add((i.pow(Integer.parseInt(i.toString()))));
  19.         }
  20.        
  21.         String s1 = total.toString();        
  22.        
  23.         System.out.println("The answer to the question is: " +
  24.                 s1.substring(s1.length()-10));
  25.     }
  26. }
  27. /* Output
  28.  * The answer to the question is: 9110846700
  29.  */
Advertisement
Add Comment
Please, Sign In to add comment