| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/big" |
| 5 |
) |
| 6 |
|
| 7 |
var ( |
| 8 |
_D = []*big.Int{big.NewInt(3), big.NewInt(4), big.NewInt(7), big.NewInt(8), big.NewInt(11), big.NewInt(19), big.NewInt(43), big.NewInt(67), big.NewInt(163)} |
| 9 |
one = big.NewInt(1) |
| 10 |
two = big.NewInt(2) |
| 11 |
) |
| 12 |
|
| 13 |
func test(t *big.Int, q *big.Int) bool { |
| 14 |
for _, d := range _D { |
| 15 |
q.Exp(t, two, nil) |
| 16 |
q.Add(q, d) |
| 17 |
if q.Bits()[0]&3 != 0 { |
| 18 |
continue |
| 19 |
} |
| 20 |
q.Rsh(q, 2) |
| 21 |
|
| 22 |
if q.ProbablyPrime(20) { |
| 23 |
return true |
| 24 |
} |
| 25 |
} |
| 26 |
return false |
| 27 |
} |
| 28 |
|
| 29 |
func nextSiec(M *big.Int) (q, t *big.Int) { |
| 30 |
t = new(big.Int) |
| 31 |
q = new(big.Int) |
| 32 |
t.Lsh(M, 2) |
| 33 |
t.Sub(t, _D[8]) |
| 34 |
t.Sqrt(t) |
| 35 |
t.Sub(t, one) |
| 36 |
for { |
| 37 |
if test(t, q) && q.Cmp(M) > 0 { |
| 38 |
return |
| 39 |
} |
| 40 |
t.Add(t, one) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func prevSiec(M *big.Int) (q, t *big.Int) { |
| 45 |
t = new(big.Int) |
| 46 |
q = new(big.Int) |
| 47 |
t.Lsh(M, 2) |
| 48 |
t.Sub(t, _D[0]) |
| 49 |
t.Sqrt(t) |
| 50 |
t.Add(t, one) |
| 51 |
for { |
| 52 |
if test(t, q) && q.Cmp(M) < 0 { |
| 53 |
return |
| 54 |
} |
| 55 |
t.Sub(t, one) |
| 56 |
} |
| 57 |
} |