-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler16.java
More file actions
44 lines (41 loc) · 845 Bytes
/
euler16.java
File metadata and controls
44 lines (41 loc) · 845 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
34
35
36
37
38
39
40
41
42
43
44
/* What is the sum of the digits of the number 2^1000? */
import java.util.ArrayList;
class Euler16
{
public static void main(String argz[])
{
ArrayList<Integer> twos = new ArrayList<Integer>();
int carryflag = 0;
twos.add(1);
for(int i=0; i<1000; i++)
{
for(int j=twos.size()-1; j>=0; j--)
{
if(twos.get(j)>=5)
{
twos.set(j,(twos.get(j)*2 + carryflag)%10);
carryflag = 0;
if(j==0)
{
twos.add(0,1);
}
else
{
carryflag = 1;
}
}
else
{
twos.set(j, twos.get(j)*2 + carryflag);
carryflag = 0;
}
}
}
int totalsum = 0;
for(int digit:twos)
{
totalsum += digit;
}
System.out.println(totalsum);
}
}