-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime.go
209 lines (186 loc) · 3.6 KB
/
prime.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package prime
import (
"math"
)
/*
Empty list
*/
func factorsOfOne(n int) []int {
factors := make([]int, 0)
return factors
}
/*
Just n == 2 works but n > 1 is more generic
func factorsOfTwo(n int) []int {
factors := make([]int, 0)
if n == 2 {
factors = append(factors, 2)
}
return factors
}
*/
func factorsOfTwo(n int) []int {
factors := make([]int, 0)
if n > 1 {
factors = append(factors, 2)
}
return factors
}
/*
Change appending 2 to appending n
*/
func factorsOfThree(n int) []int {
factors := make([]int, 0)
if n > 1 {
factors = append(factors, n)
}
return factors
}
/*
Second if n > 1 can be inside or outside first one
func factorsOfFour(n int) []int {
factors := make([]int, 0)
if n > 1 {
if n%2 == 0 {
factors = append(factors, 2)
n = n / 2
}
if n > 1 {
factors = append(factors, n)
}
}
return factors
}
Works for 4,5,6,7
*/
func factorsOfFour(n int) []int {
factors := make([]int, 0)
if n > 1 {
if n%2 == 0 {
factors = append(factors, 2)
n = n / 2
}
}
if n > 1 {
factors = append(factors, n)
}
return factors
}
/*
Change if n%2 == 0 to while n%2 == 0
*/
func factorsOfEight(n int) []int {
factors := make([]int, 0)
if n > 1 {
for n%2 == 0 {
factors = append(factors, 2)
n = n / 2
}
}
if n > 1 {
factors = append(factors, n)
}
return factors
}
/*
Copy the part that factors out 2s to factor out 3s
*/
func factorsOfNine(n int) []int {
factors := make([]int, 0)
if n > 1 {
for n%2 == 0 {
factors = append(factors, 2)
n = n / 2
}
for n%3 == 0 {
factors = append(factors, 3)
n = n / 3
}
}
if n > 1 {
factors = append(factors, n)
}
return factors
}
/*
Minimal change to get factorsOfN to work.
Deduplicate the two engines that factor out 2,3 to a while loop that factors out a divisor
*/
func factorsOfNMinimal(n int) []int {
factors := make([]int, 0)
divisor := 2
for n > 1 {
for n%divisor == 0 {
factors = append(factors, divisor)
n = n / divisor
}
divisor++
}
// This if could be removed at this point
if n > 1 {
factors = append(factors, n)
}
return factors
}
/*
Refactoring
Make inner while loop a for loop
func factorsOfNine(n int) []int {
factors := make([]int, 0)
divisor := 2
for n > 1 {
for ; n%divisor == 0; n = n / divisor {
factors = append(factors, divisor)
}
divisor++
}
if n > 1 {
factors = append(factors, n)
}
return factors
}
Make outer while loop a for loop
func factorsOfNine(n int) []int {
factors := make([]int, 0)
for divisor := 2; n > 1; divisor++ {
for ; n%divisor == 0; n = n / divisor {
factors = append(factors, divisor)
}
}
if n > 1 {
factors = append(factors, n)
}
return factors
}
Then can delete the trailing if n > 1
*/
func factorsOfN(n int) []int {
factors := make([]int, 0)
for divisor := 2; n > 1; divisor++ {
for ; n%divisor == 0; n = n / divisor {
factors = append(factors, divisor)
}
}
return factors
}
/*
Termiante outer loop at square root of n.
If we reach the end of the loops and no factors are found yet then the original number must be prime.
Speeds up factorsOfN(2147483647) from 7 seconds to shorter than go test will show me 0.00s.
Speeds up factorsOfN(67280421310721) from over 2 minutes to 0.05 seconds.
*/
func factorsOfNOptimizedForLargePrimes(n int) []int {
factors := make([]int, 0)
origN := n
// Surely there's a better way to do this
sqrtN := int(math.Ceil(math.Sqrt(float64(n))))
for divisor := 2; n > 1 && divisor <= sqrtN; divisor++ {
for ; n%divisor == 0; n = n / divisor {
factors = append(factors, divisor)
}
}
if len(factors) == 0 && origN != 1 {
factors = append(factors, origN)
}
return factors
}