-
Notifications
You must be signed in to change notification settings - Fork 0
/
p50.go
62 lines (52 loc) · 946 Bytes
/
p50.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
package main
import (
"fmt"
"math"
"time"
)
func IsPrime(prime []int, x int) bool {
bound := int(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()
prime, maxP := []int{2, 3, 5, 7, 11, 13}, 13
accu := []int{0, 2, 5, 10, 17, 28, 41}
ans, maxCnt := 0, 0
for maxP < 1000000 {
maxP += 2
if IsPrime(prime, maxP) {
prime = append(prime, maxP)
accu = append(accu, maxP+accu[len(accu)-1])
}
}
for p := len(prime) - 1; p > maxCnt; p-- {
for i, j := 0, 0; i <= p && p-j > maxCnt; {
if accu[i]-accu[j] > prime[p] {
j++
} else if accu[i]-accu[j] < prime[p] {
i++
} else {
if i-j > maxCnt {
ans = prime[p]
maxCnt = i - j
}
break
}
}
}
fmt.Println(ans)
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}