-
Notifications
You must be signed in to change notification settings - Fork 0
/
p187.go
56 lines (45 loc) · 750 Bytes
/
p187.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
package main
import (
"fmt"
"math"
"time"
)
func IsPrime(prime []int64, x int64) bool {
bound := int64(math.Sqrt(float64(x)))
if x%bound == 0 {
return false
}
for _, p := range prime {
if p > bound {
return true
}
if x%p == 0 {
return false
}
}
return true
}
func main() {
start := time.Now()
limit := int64(100000000)
prime, maxP, tail := []int64{2, 3}, int64(3), 1
ans := 3
for maxP < limit/2 {
maxP += 2
if IsPrime(prime, maxP) {
prime = append(prime, maxP)
if maxP*maxP < limit {
ans += len(prime)
tail++
} else {
for prime[tail]*maxP >= limit {
tail--
}
ans += tail + 1
}
}
}
fmt.Println(ans)
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}