-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem016.java
More file actions
33 lines (28 loc) · 817 Bytes
/
problem016.java
File metadata and controls
33 lines (28 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package project_beuler;
public class problem016 {
// 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
//
// What is the sum of the digits of the number 2^1000?
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 1000;
String start = "1";
for(int i = 0; i < n; i++) {
start = Double(start);
}
int sum = 0;
for(int x = 0; x < start.length(); x++)
sum+= Integer.parseInt(""+start.charAt(x));
System.out.println(sum);
}
public static String Double(String n) {
String number = "";
int carryover = 0;
for(int y = n.length()-1; y >= 0; y--) { //columns
int temp = 2* Integer.parseInt(n.charAt(y)+"") +carryover;
number = (temp%10)+""+number;
carryover = temp/10;
}
return (carryover != 0)? carryover + number : number;
}
}