-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler045.go
73 lines (66 loc) · 1.39 KB
/
euler045.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"math"
)
func GenTriangleNums(output chan<- int) {
GenSequence(func(a int) int {
return a * (a + 1) / 2
}, output)
}
func GenPentagonNums(output chan<- int) {
GenSequence(func(a int) int {
return a * (3*a - 1) / 2
}, output)
}
func GenHexNums(output chan<- int) {
GenSequence(func(a int) int {
return a * (2*a - 1)
}, output)
}
func GenSequence(f func(int) int, output chan<- int) {
for x := 1; x < int(math.MaxInt32); x++ {
output <- f(x)
}
close(output)
}
func main() {
a := make(chan int)
b := make(chan int)
c := make(chan int)
go GenPentagonNums(a)
go GenHexNums(b)
go GenTriangleNums(c)
curPent := <-a
curHex := <-b
curTri := <-c
curPent = <-a
curHex = <-b
curTri = <-c
fmt.Println(curTri, curPent, curHex)
for curPent != curTri || curTri != curHex {
//Determine the smallest num and get the next num
if curTri <= curPent && curTri <= curHex {
curTri = <-c
} else if curHex <= curPent && curHex <= curTri {
curHex = <-b
} else {
curPent = <-a
}
}
fmt.Println(curTri, curPent, curHex)
curPent = <-a
curHex = <-b
curTri = <-c
for curPent != curTri || curTri != curHex {
//Determine the smallest num and get the next num
if curTri <= curPent && curTri <= curHex {
curTri = <-c
} else if curHex <= curPent && curHex <= curTri {
curHex = <-b
} else {
curPent = <-a
}
}
fmt.Println(curTri, curPent, curHex)
}