forked from sw0x2A/project-euler-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0013.go
49 lines (45 loc) · 938 Bytes
/
0013.go
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
// https://projecteuler.net/problem=13
package main
import (
"io/ioutil"
"math/big"
"strconv"
"strings"
)
func p13() string {
fileBuf, err := ioutil.ReadFile("0013_input.txt")
if err != nil {
panic(err)
}
fileStr := string(fileBuf)
lines := strings.SplitN(fileStr, "\n", 100)
result := ""
carryout, digit := 0, 0
for i := 49; i >= 0; i-- {
sum := carryout
for j := range lines {
num, _ := strconv.Atoi(string(lines[j][i]))
sum += num
}
digit = sum % 10
carryout = sum / 10
result = strconv.Itoa(digit) + result
}
result = strconv.Itoa(carryout) + result
return result[0:10]
}
func p13big() string {
fileBuf, err := ioutil.ReadFile("0013_input.txt")
if err != nil {
panic(err)
}
fileStr := string(fileBuf)
s := strings.Split(fileStr, "\n")
n := new(big.Int).SetInt64(0)
for _, v := range s {
if t, ok := new(big.Int).SetString(v, 10); ok {
n.Add(n, t)
}
}
return n.String()[0:10]
}