MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / PollardsRhoFactorization

Function PollardsRhoFactorization

math/pollard.go:31–45  ·  view source on GitHub ↗

PollardsRhoFactorization is an implementation of Pollard's rho factorization algorithm using the default parameters x = y = 2

(n *big.Int, f func(n *big.Int) func(x *big.Int) *big.Int)

Source from the content-addressed store, hash-verified

29// PollardsRhoFactorization is an implementation of Pollard's rho factorization algorithm
30// using the default parameters x = y = 2
31func PollardsRhoFactorization(n *big.Int, f func(n *big.Int) func(x *big.Int) *big.Int) (*big.Int, error) {
32 x, y, d := big.NewInt(2), big.NewInt(2), big.NewInt(1)
33 bigOne := big.NewInt(1)
34 g := f(n)
35 for d.Cmp(bigOne) == 0 {
36 x = g(x)
37 y = g(g(y))
38 sub := new(big.Int).Sub(x, y)
39 d.GCD(nil, nil, sub.Abs(sub), n)
40 }
41 if d.Cmp(n) == 0 {
42 return nil, errors.New("factorization failed")
43 }
44 return d, nil
45}

Callers 2

TestRhoFunction · 0.85
BenchmarkRhoFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestRhoFunction · 0.68
BenchmarkRhoFunction · 0.68