File tree Expand file tree Collapse file tree 1 file changed +11
-16
lines changed
cluster/loadbalance/maglevconsistenthashing Expand file tree Collapse file tree 1 file changed +11
-16
lines changed Original file line number Diff line number Diff line change 18
18
package maglevconsistenthashing
19
19
20
20
import (
21
+ "math"
22
+
21
23
"github.com/cespare/xxhash/v2"
22
24
23
25
"dubbo.apache.org/dubbo-go/v3/protocol"
@@ -109,14 +111,15 @@ func isPrime(num int) bool {
109
111
if num < 2 {
110
112
return false
111
113
}
112
- if num <= 3 {
114
+ if num == 2 {
113
115
return true
114
116
}
115
- if num % 2 == 0 || num % 3 == 0 {
117
+ if num % 2 == 0 {
116
118
return false
117
119
}
118
- for i := 5 ; i * i <= num ; i += 6 {
119
- if num % i == 0 || num % (i + 2 ) == 0 {
120
+ sqrt := int (math .Sqrt (float64 (num )))
121
+ for i := 3 ; i <= sqrt ; i += 2 {
122
+ if num % i == 0 {
120
123
return false
121
124
}
122
125
}
@@ -125,17 +128,9 @@ func isPrime(num int) bool {
125
128
126
129
// NextPrime 函数用于找出比 n 大的第一个素数
127
130
func NextPrime (n int ) int {
128
- if n <= 2 {
129
- return 2
130
- }
131
- num := n
132
- if num % 2 == 0 {
133
- num ++
134
- } else {
135
- num += 2
136
- }
137
- for ! isPrime (num ) {
138
- num += 2
131
+ for num := n + 1 ; ; num ++ {
132
+ if isPrime (num ) {
133
+ return num
134
+ }
139
135
}
140
- return num
141
136
}
You can’t perform that action at this time.
0 commit comments