diff --git a/cmd/primitiveRoot.go b/cmd/primitiveRoot.go index c6c9961..99b8eb0 100644 --- a/cmd/primitiveRoot.go +++ b/cmd/primitiveRoot.go @@ -33,6 +33,16 @@ func computeNaive(modulus *big.Int) (*big.Int, error) { return big.NewInt(0), nil } + coprimes := make(map[string]bool) + for a := big.NewInt(1); a.Cmp(modulus) == -1; a.Add(a, big.NewInt(1)) { + gcd := new(big.Int).GCD(nil, nil, a, modulus) + if gcd.Cmp(big.NewInt(1)) != 0 { + continue + } + + coprimes[a.Text(10)] = true + } + for g := big.NewInt(1); g.Cmp(modulus) == -1; g.Add(g, big.NewInt(1)) { e := big.NewInt(1) exps := make(map[string]big.Int) @@ -43,13 +53,8 @@ func computeNaive(modulus *big.Int) (*big.Int, error) { } isPrimitive := true - for a := big.NewInt(1); a.Cmp(modulus) == -1; a.Add(a, big.NewInt(1)) { - gcd := new(big.Int).GCD(nil, nil, a, modulus) - if gcd.Cmp(big.NewInt(1)) != 0 { - continue - } - - _, ok := exps[a.Text(10)] + for a := range coprimes { + _, ok := exps[a] if !ok { isPrimitive = false break