-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion16.java
More file actions
60 lines (48 loc) · 1.04 KB
/
Question16.java
File metadata and controls
60 lines (48 loc) · 1.04 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Arrays;
/**
*
* 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 class Question16 {
static int LEN = 1000000;
static int res[] = new int[LEN];
static int curr_pos = 0; // current highest bit
public static void init() {
Arrays.fill(res, 0);
res[0] = 1;
curr_pos = 1;
}
// res *= 2
public static void time_two() {
int tmp_bit = 0; // puls one to next digit?
int i = 0, result = 0;
for (i = 0; i < curr_pos + 1; i++) {
result = res[i] * 2 + tmp_bit;
tmp_bit = result / 10;
res[i] = result % 10;
if (tmp_bit != 0)
curr_pos++;
}
}
public static void calculate(int power) {
int i = 0;
for (i = 0; i < power; i++)
time_two();
}
public static double get_result() {
double result = 0;
int i = 0;
for (i = 0; i < LEN; i++) {
result += res[i];
}
return result;
}
public static void main(String[] args) {
init();
calculate(1000);
System.out.println("The result is " + get_result());
}
}