lordmat

Problem 16

Jun 14th, 2011
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Mat
  4.  */
  5. package problem16;
  6.  
  7. import java.math.BigInteger;
  8.  
  9. public class twopow1000 {
  10.  
  11.     public static void main(String[] args) {
  12.         BigInteger number2 = new BigInteger("2");
  13.        
  14.         final int PRODUCTSIZE = 1000;
  15.        
  16.         /* Set as one so productsize doesn't need to be one less
  17.          * This loop goes through doing n = n*2 as many times as the product size          
  18.          */
  19.         for(BigInteger i = BigInteger.ONE;
  20.                 i.compareTo(BigInteger.valueOf(PRODUCTSIZE)) < 0; i = i.add(BigInteger.ONE)){
  21.             number2 = number2.multiply(BigInteger.valueOf(2));
  22.         }
  23.        
  24.         System.out.println("Size of number2 is: " + number2.toString());
  25.        
  26.        
  27.         String s1 = number2.toString();
  28.         int totalnum = 0;
  29.        
  30.         // Loop adding digits together into totalnum
  31.         for(int i = 0; i < s1.length();i++){
  32.             totalnum+= Integer.parseInt(""+s1.charAt(i));
  33.         }
  34.         System.out.println("Added together is " + totalnum);
  35.  
  36.     }
  37. }
  38. /* Output
  39.  *
  40.  * Size of number2 is: 107150860718626732094842504906000181056140481170553360744
  41.  * 37503883703510511249361224931983788156958581275946729175531468251871452856923
  42.  * 14043598457757469857480393456777482423098542107460506237114187795418215304647
  43.  * 49835819412673987675591655439460770629145711964776865421676604298316526243868
  44.  * 37205668069376
  45.  * Added together is 1366 *  
  46.  *
  47.  */
Advertisement
Add Comment
Please, Sign In to add comment