-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime.go
162 lines (133 loc) · 2.61 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
package main
import (
"errors"
"log"
"math/big"
"sort"
"sync"
"time"
)
const (
timeout = 20 * time.Second
)
var big0 = big.NewInt(0)
var big2 = big.NewInt(2)
var maxCacheNumber = big.NewInt(1000 * 1000)
type Prime struct {
primes []*big.Int
index int
maxPrime *big.Int
mutex sync.Mutex
}
func NewPrime() *Prime {
prime := &Prime{
primes: []*big.Int{
big.NewInt(2),
big.NewInt(3),
},
maxPrime: big.NewInt(2),
}
go prime.start()
return prime
}
func (prime *Prime) start() {
for p := big.NewInt(3); p.Cmp(maxCacheNumber) < 0; {
p.Add(p, big2)
b, err := prime.isPrime(p, time.Now().Add(timeout))
if err != nil {
log.Printf("%v", err)
return
}
if !b {
continue
}
prime.mutex.Lock()
prime.primes = append(prime.primes, newInt(p))
prime.mutex.Unlock()
}
}
func (prime *Prime) Next() (*big.Int, error) {
prime.mutex.Lock()
primes := prime.primes[:]
prime.mutex.Unlock()
if prime.index < len(primes) {
p := primes[prime.index]
prime.index++
return p, nil
}
expire := time.Now().Add(timeout)
lastPrime := last(primes)
if lastPrime.Cmp(prime.maxPrime) > 0 {
prime.maxPrime = newInt(lastPrime)
}
q := newInt(prime.maxPrime)
q.Add(q, big2)
for {
b, err := prime.isPrime(q, expire)
if err != nil {
return nil, err
}
if b {
prime.maxPrime = q
prime.index++
return newInt(q), nil
}
q.Add(q, big2)
}
}
func (prime *Prime) IsPrime(n *big.Int) (bool, error) {
return prime.isPrime(n, time.Now().Add(timeout))
}
func (prime *Prime) isPrime(n *big.Int, expire time.Time) (bool, error) {
prime.mutex.Lock()
primes := prime.primes[:]
prime.mutex.Unlock()
if n.Cmp(last(primes)) > 0 {
return isPrime(n, primes, expire)
}
prime.mutex.Lock()
defer prime.mutex.Unlock()
i := sort.Search(len(prime.primes), func(i int) bool {
return prime.primes[i].Cmp(n) >= 0
})
if i < len(prime.primes) && prime.primes[i].Cmp(n) == 0 {
return true, nil
} else {
return false, nil
}
}
func isPrime(n *big.Int, primes []*big.Int, expire time.Time) (bool, error) {
m := new(big.Int)
m.Sqrt(n)
r := new(big.Int)
for _, p := range primes {
if p.Cmp(m) > 0 {
return true, nil
}
if r.Mod(n, p).Cmp(big0) == 0 {
return false, nil
}
}
q := new(big.Int)
q.Add(last(primes), big2)
for ; q.Cmp(m) <= 0; q.Add(q, big2) {
if time.Now().After(expire) {
return false, errors.New("timeout")
}
if r.Mod(n, q).Cmp(big0) == 0 {
return false, nil
}
}
return true, nil
}
func last(s []*big.Int) *big.Int {
if len(s) == 0 {
return nil
}
return s[len(s)-1]
}
func newInt(i *big.Int) *big.Int {
bi := new(big.Int)
bi.Set(i)
return bi
}