-
Notifications
You must be signed in to change notification settings - Fork 33
/
primes.go
61 lines (52 loc) · 822 Bytes
/
primes.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
package main
import (
"fmt"
"math"
"os"
"time"
)
func getPrimes7(n int) []int {
if n < 2 {
return []int{}
}
if n == 2 {
return []int{2}
}
s := []int{}
for i := 3; i <= n; i += 2 {
s = append(s, i)
}
var j int
m := 3
mroot := int(math.Sqrt(float64(n)))
half := len(s)
for i := 0; m <= mroot; {
if s[i] != 0 {
j = (m*m - 3) / 2
s[j] = 0
for j < half {
s[j] = 0
j += m
}
}
i++
m = 2*i + 3
}
res := []int{}
res = append(res, 2)
for _, v := range s {
if v != 0 {
res = append(res, v)
}
}
return res
}
func main() {
var startTime = time.Now()
var periodTime, _ = time.ParseDuration(os.Getenv("RUN_TIME") + "s")
var res []int
for time.Since(startTime) < periodTime {
res = getPrimes7(10000000)
fmt.Printf("Found %d prime numbers.\n", len(res))
}
}