-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverage.html
209 lines (179 loc) · 5.95 KB
/
coverage.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>prime: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">prime/nth_prime.go (100.0%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">no coverage</span>
<span class="cov1">low coverage</span>
<span class="cov2">*</span>
<span class="cov3">*</span>
<span class="cov4">*</span>
<span class="cov5">*</span>
<span class="cov6">*</span>
<span class="cov7">*</span>
<span class="cov8">*</span>
<span class="cov9">*</span>
<span class="cov10">high coverage</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">// Package prime determines the nth prime.
package prime
import (
"errors"
"math"
"sort"
)
// Nth returns the nth prime number. An error must be returned if the nth prime number can't be calculated ('n' is equal or less than zero)
func Nth(input int) (int, error) <span class="cov3" title="16">{
if input < 1 </span><span class="cov1" title="2">{
return 0, errors.New("the nth prime has to be equal to or greater than 1")
}</span>
<span class="cov2" title="14">primes := []int{}
var prime int
// Using a large size to minimize the number of loops since each loop is
// expensive.
var searchSize int = input * 11
for len(primes) <= input </span><span class="cov2" title="14">{
primes = append(primes, findPrimeWithSieve(input, searchSize)...)
prime = primes[input-1]
searchSize *= 2
}</span>
<span class="cov2" title="14">return prime, nil</span>
}
// findPrimeWithSieve returns a list of primes up to the nth prime.
func findPrimeWithSieve(searchTarget int, limit int) []int <span class="cov2" title="14">{
primes := []int{}
// If limit is greater than 2 and is even, substract 1.
if limit > 2 && limit%2 == 0 </span><span class="cov2" title="7">{
limit--
}</span>
// Index 2 to limit. These need to all be set to true.
<span class="cov2" title="14">numbers := map[int]bool{}
// - Only generate a list for '2' and odd numbers. All other even numbers
// aren't primes so they're automatically set to false so don't bother
// with them.
if limit >= 2 </span><span class="cov2" title="14">{
numbers[2] = true
}</span>
<span class="cov2" title="14">for i := 3; i <= limit; i += 2 </span><span class="cov8" title="55347">{
numbers[i] = true
}</span>
<span class="cov2" title="14">for number, isPrime := range numbers </span><span class="cov9" title="87932">{
if isPrime </span><span class="cov8" title="23214">{
for x, j := 1, number*number; j <= limit; x, j = x+1, number*number+x*number </span><span class="cov10" title="234792">{
numbers[j] = false
}</span>
}
}
<span class="cov2" title="14">outer := 2
sqrtLimit := int(math.Sqrt(float64(limit))) + 1
var loop int
var square int
var inner int
for outer <= sqrtLimit </span><span class="cov5" title="415">{
if numbers[outer] </span><span class="cov4" title="113">{
loop = 2
square = outer * outer
inner = square * (outer * (loop - 2))
for inner <= limit </span><span class="cov8" title="19430">{
delete(numbers, inner)
loop++
inner = square * (outer * (loop - 2))
}</span>
}
<span class="cov5" title="415">outer++</span>
}
<span class="cov2" title="14">for number, isPrime := range numbers </span><span class="cov9" title="92101">{
if isPrime </span><span class="cov7" title="10654">{
primes = append(primes, number)
}</span>
}
<span class="cov2" title="14">sort.Ints(primes)
return primes</span>
}
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>