-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiftyDigitSum.java
56 lines (50 loc) · 1.23 KB
/
FiftyDigitSum.java
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
package Euler;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigInteger;
public class FiftyDigitSum {
/**
* @param args
*/
public static void main(String[] args) {
BigInteger[] numbers = readNumbers();
BigInteger sum = BigInteger.ZERO;
for(int i =0;i<100;i++){
sum = sum.add(numbers[i]);
}
String str = sum.toString();
System.out.println(sum);
System.out.println(str.substring(0,10));
}
public static BigInteger[] readNumbers() {
BigInteger[] numbers = new BigInteger[100];
Reader fr = null;
try {
fr = new InputStreamReader(new FileInputStream("numbers"));
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
int i = 0;
while (line != null) {
line = line.trim();
if(line.isEmpty()){
continue;
}
System.out.println(line);
numbers[i] = new BigInteger(line);
i++;
line = reader.readLine();
}
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return numbers;
}
}