/
pminus1.go
47 lines (36 loc) · 842 Bytes
/
pminus1.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
package factor
import (
"math/big"
)
func PMinus1(n *big.Int) (primes, composites []*big.Int) {
if n.ProbablyPrime(10) {
return []*big.Int{n}, nil
}
a := big.NewInt(2 * 3 * 5 * 7 * 11)
var one big.Int
one.SetInt64(1)
var bigp big.Int
var m big.Int
for i, p := range smallPrimes {
bigp.SetInt64(p)
m.SetInt64(1)
for n.Cmp(&m) == 1 {
m.Mul(&m, &bigp)
}
// went one too far
m.Div(&m, &bigp)
a.Exp(a, &m, n)
if i > 0 && i%16 == 0 {
var aMinus1 big.Int
aMinus1.Sub(a, &one)
if newN, newG, ok := checkGCD(n, &aMinus1); ok {
pr, co := PMinus1(newN)
primes, composites = append(primes, pr...), append(composites, co...)
pr, co = PMinus1(newG)
primes, composites = append(primes, pr...), append(composites, co...)
return primes, composites
}
}
}
return nil, []*big.Int{n}
}