Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Mat
- */
- package problem16;
- import java.math.BigInteger;
- public class twopow1000 {
- public static void main(String[] args) {
- BigInteger number2 = new BigInteger("2");
- final int PRODUCTSIZE = 1000;
- /* Set as one so productsize doesn't need to be one less
- * This loop goes through doing n = n*2 as many times as the product size
- */
- for(BigInteger i = BigInteger.ONE;
- i.compareTo(BigInteger.valueOf(PRODUCTSIZE)) < 0; i = i.add(BigInteger.ONE)){
- number2 = number2.multiply(BigInteger.valueOf(2));
- }
- System.out.println("Size of number2 is: " + number2.toString());
- String s1 = number2.toString();
- int totalnum = 0;
- // Loop adding digits together into totalnum
- for(int i = 0; i < s1.length();i++){
- totalnum+= Integer.parseInt(""+s1.charAt(i));
- }
- System.out.println("Added together is " + totalnum);
- }
- }
- /* Output
- *
- * Size of number2 is: 107150860718626732094842504906000181056140481170553360744
- * 37503883703510511249361224931983788156958581275946729175531468251871452856923
- * 14043598457757469857480393456777482423098542107460506237114187795418215304647
- * 49835819412673987675591655439460770629145711964776865421676604298316526243868
- * 37205668069376
- * Added together is 1366 *
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment