/
numberCruncher.go
191 lines (167 loc) · 4.64 KB
/
numberCruncher.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"fmt"
"flag"
"math/big"
"strings"
"strconv"
"github.com/fatih/set"
"github.com/Arcania0311/numberCruncher/crunch"
)
/* Variables used for command line parsing.
*/
var (
printArray bool
printProgress bool
goal int
)
// Where the magic happens.
func main() {
// Parse command line flags
flag.BoolVar(&printArray, "array", false, "Output an array of path lengths rather than all paths.")
flag.BoolVar(&printProgress, "progress", false, "Display notification when a new int has been found.")
flag.IntVar(&goal, "goal", 100, "How many integers should be found before aborting.")
flag.Parse()
// Create the initial set and a set to store all found numbers
initialNumbers := set.New(4)
allNumbers := set.New()
// Use a map to store the numbers we're interested in
crunchedNumbers := make(map[int]*crunch.CrunchedNumber)
// Create two channels, one 'small' numbers and one for big ones
channelBig := make(chan *crunch.CrunchedNumber, 100)
channelSmall := make(chan *crunch.CrunchedNumber, 100)
// Add the first number to the small channel
for !initialNumbers.IsEmpty() {
x := initialNumbers.Pop()
temp := x.(int)
i := int64(temp)
firstNumber := &crunch.CrunchedNumber{big.NewInt(i), ""}
channelSmall <- firstNumber
}
// Loop while less then 100 values have been found
found := 0
for found < goal {
select {
case nextNumber := <- channelSmall:
// Convert it to an int and check whether it is in the 0-100 range
// If so, add it to the crunchedNumbers map
temp := nextNumber.N.Int64()
if 0 < temp && temp <= 100 {
value, ok := crunchedNumbers[int(temp)]
if !ok {
found++
if printProgress {
fmt.Println(nextNumber.N, " as element : " ,found)
}
crunchedNumbers[int(temp)] = nextNumber
} else {
oldNumber := strings.NewReader(value.How)
newNumber := strings.NewReader(nextNumber.How)
if oldNumber.Len() > newNumber.Len() {
crunchedNumbers[int(temp)] = nextNumber
}
}
}
// If it has already been found, skip it, else add it to numbers found
if allNumbers.Has(nextNumber.N.String()) {
continue
}
allNumbers.Add(nextNumber.N.String())
go crunch.AddFactorial(nextNumber, channelBig, channelSmall)
crunch.AddSqrt(nextNumber, channelBig, channelSmall)
case nextNumber := <- channelBig:
// If it has already been found, skip it, else add it to numbers found
if allNumbers.Has(nextNumber.N.String()) {
continue
}
allNumbers.Add(nextNumber.N.String())
crunch.AddSqrt(nextNumber, channelBig, channelSmall)
}
}
// End of program
foundSlice := make([]int, 100)
for x := 0; x < 100; x++ {
value, ok := crunchedNumbers[x + 1]
if ok {
path, pathLength := PrintRoute(value)
if !printArray {
fmt.Println(x + 1, path)
}
foundSlice[x] = pathLength
} else {
foundSlice[x] = 0
}
}
if printArray {
// Print all found pathlengths at the end, wee bit hacky
fmt.Print("[")
for i, v := range foundSlice {
fmt.Print(v)
if i != 99 {
fmt.Print(", ")
}
}
fmt.Println("]")
}
}
/* Returns a string with a human friendly version of the path, plus the length
* of the path.
*/
func PrintRoute(r *crunch.CrunchedNumber) (string, int) {
reader := strings.NewReader(r.How)
sqrt := false
sqrtCount := 0
fact := false
factCount := 0
path := ": 4"
pathLength := 0
for i := 0; i < reader.Len(); i++ {
switch r.How[i] {
case 'f':
if sqrt {
sqrt = false
path = path + strconv.Itoa(sqrtCount) + "s" + " 1f"
pathLength += sqrtCount + 1
sqrtCount = 0
}
if fact {
factCount++
} else {
path = path + " "
factCount = 1
fact = true
}
case 's':
if fact {
fact = false
path = path + strconv.Itoa(factCount) + "!"
pathLength += factCount
factCount = 0
}
if sqrt {
sqrtCount++
} else {
path = path + " "
sqrtCount = 1
sqrt = true
}
}
}
// Append the end of the path
if sqrt {
path = path + strconv.Itoa(sqrtCount) + "s"
// Hacky 'solution' for the path of 2
pathLength += sqrtCount
if pathLength != 1 {
pathLength++
path = path + " 1f"
}
}
if fact {
path = path + strconv.Itoa(factCount) + "!"
pathLength += factCount
}
// Prepend the path length
path = "(" + strconv.Itoa(pathLength) + ") " + path
return path, pathLength
}