-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler044.go
53 lines (46 loc) · 987 Bytes
/
euler044.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
50
51
52
53
package main
import (
"fmt"
"math"
)
var (
MappedVals map[int64]struct{}
ArrOfVals []int64
)
func GeneratePentagonalNumbers(c chan<- int64) {
for n := int64(1); true; n++ {
c <- ((n * ((3 * n) - 1)) / 2)
}
}
func main() {
CurMin := int64(math.MaxInt64)
MappedVals = make(map[int64]struct{}, 0)
ArrOfVals = make([]int64, 0)
PentagonalNumChan := make(chan int64)
go GeneratePentagonalNumbers(PentagonalNumChan)
x := <-PentagonalNumChan
MappedVals[x] = struct{}{}
ArrOfVals = append(ArrOfVals, x)
for ph := range PentagonalNumChan {
if ph-ArrOfVals[len(ArrOfVals)-1] > CurMin {
fmt.Println(CurMin)
return
}
for _, pj := range ArrOfVals {
pk := ph - pj
if _, exists := MappedVals[pk]; exists {
d := pk - pj
if d < 0 {
d *= -1
}
if _, exists := MappedVals[d]; exists {
CurMin = d
fmt.Println(CurMin)
}
}
}
//fmt.Println(len(ArrOfVals))
MappedVals[ph] = struct{}{}
ArrOfVals = append(ArrOfVals, ph)
}
}