-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem14.go
More file actions
62 lines (57 loc) · 1.43 KB
/
Copy pathproblem14.go
File metadata and controls
62 lines (57 loc) · 1.43 KB
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
package projecteuler
import "sync"
// holds property of collatz chain
// i.e. initial number of chain & length of chain
// produced ( numbers generated before we reach 1,
// because it's thought that all collatz seqs end at 1 )
type chainProp struct {
init int
length int
}
// generates next term of collatz sequence, given current term
func generateNextTermOfCollatz(n int) int {
if n%2 == 0 {
return n / 2
}
return 3*n + 1
}
// generates collatz seqeuence for a given starting number
// and keeps track length of chain upto this point
//
// communicates with listener go-routine via channel
func generateCollatzSeq(cp chainProp, channel chan chainProp) {
for n := cp.init; n != 1; {
n = generateNextTermOfCollatz(n)
cp.length++
}
channel <- cp
}
// LongestCollatzSeq - finds longest collatz sequence for a certain initial number
// i.e. for which initial number under 10^6, this sequence is of length max.
//
// deploying 10^6 lightweight go-routines, for computing desired value at
// lightning fast speed
func LongestCollatzSeq() int {
maxChain := chainProp{1, 1}
var wg sync.WaitGroup
channel := make(chan chainProp, 2)
wg.Add(1)
go func() {
c := 0
for v := range channel {
if v.length > maxChain.length {
maxChain = v
}
c++
if c == 999999 {
close(channel)
wg.Done()
}
}
}()
for cur := 1; cur < 1000000; cur++ {
go generateCollatzSeq(chainProp{cur, 1}, channel)
}
wg.Wait()
return maxChain.init
}