galois.primitive_root¶
- galois.primitive_root(n, start=1, stop=None, reverse=False)¶
Finds the smallest primitive root modulo \(n\).
\(g\) is a primitive root if the totatives of \(n\), the positive integers \(1 \le a < n\) that are coprime with \(n\), can be generated by powers of \(g\).
Alternatively said, \(g\) is a primitive root modulo \(n\) if and only if \(g\) is a generator of the multiplicative group of integers modulo \(n\), \(\mathbb{Z}{_n^\times}\). That is, \(\mathbb{Z}{_n^\times} = \{g, g^2, \dots, g^k\}\), where \(k\) is order of the group. The order of the group \(\mathbb{Z}{_n^\times}\) is defined by Euler’s totient function, \(\phi(n) = k\). If \(\mathbb{Z}{_n^\times}\) is cyclic, the number of primitive roots modulo \(n\) is given by \(\phi(k)\) or \(\phi(\phi(n))\).
See
galois.is_cyclic
.- Parameters
n (int) – A positive integer.
start (int, optional) – Starting value (inclusive) in the search for a primitive root. The default is
1
. The resulting primitive root, if found, will be \(\textrm{start} \le g < \textrm{stop}\).stop (int, optional) – Stopping value (exclusive) in the search for a primitive root. The default is
None
which corresponds ton
. The resulting primitive root, if found, will be \(\textrm{start} \le g < \textrm{stop}\).reverse (bool, optional) – Search for a primitive root in reverse order, i.e. find the largest primitive root first. Default is
False
.
- Returns
The smallest primitive root modulo \(n\). Returns
None
if no primitive roots exist.- Return type
References
Shoup. Searching for primitive roots in finite fields. https://www.ams.org/journals/mcom/1992-58-197/S0025-5718-1992-1106981-9/S0025-5718-1992-1106981-9.pdf
Hua. On the least primitive root of a prime. https://www.ams.org/journals/bull/1942-48-10/S0002-9904-1942-07767-6/S0002-9904-1942-07767-6.pdf
http://www.numbertheory.org/courses/MP313/lectures/lecture7/page1.html
Examples
Here is an example with one primitive root, \(n = 6 = 2 * 3^1\), which fits the definition of cyclicness, see
galois.is_cyclic
. Because \(n = 6\) is not prime, the primitive root isn’t a multiplicative generator of \(\mathbb{Z}/\textbf{n}\mathbb{Z}\).In [1]: n = 6 In [2]: root = galois.primitive_root(n); root Out[2]: 5 # The congruence class coprime with n In [3]: Znx = set([a for a in range(1, n) if math.gcd(n, a) == 1]); Znx Out[3]: {1, 5} # Euler's totient function counts the "totatives", positive integers coprime with n In [4]: phi = galois.euler_totient(n); phi Out[4]: 2 In [5]: len(Znx) == phi Out[5]: True # The primitive roots are the elements in Znx that multiplicatively generate the group In [6]: for a in Znx: ...: span = set([pow(a, i, n) for i in range(1, phi + 1)]) ...: primitive_root = span == Znx ...: print("Element: {}, Span: {:<6}, Primitive root: {}".format(a, str(span), primitive_root)) ...: Element: 1, Span: {1} , Primitive root: False Element: 5, Span: {1, 5}, Primitive root: True
Here is an example with two primitive roots, \(n = 7 = 7^1\), which fits the definition of cyclicness, see
galois.is_cyclic
. Since \(n = 7\) is prime, the primitive root is a multiplicative generator of \(\mathbb{Z}/\textbf{n}\mathbb{Z}\).In [7]: n = 7 In [8]: root = galois.primitive_root(n); root Out[8]: 3 # The congruence class coprime with n In [9]: Znx = set([a for a in range(1, n) if math.gcd(n, a) == 1]); Znx Out[9]: {1, 2, 3, 4, 5, 6} # Euler's totient function counts the "totatives", positive integers coprime with n In [10]: phi = galois.euler_totient(n); phi Out[10]: 6 In [11]: len(Znx) == phi Out[11]: True # The primitive roots are the elements in Znx that multiplicatively generate the group In [12]: for a in Znx: ....: span = set([pow(a, i, n) for i in range(1, phi + 1)]) ....: primitive_root = span == Znx ....: print("Element: {}, Span: {:<18}, Primitive root: {}".format(a, str(span), primitive_root)) ....: Element: 1, Span: {1} , Primitive root: False Element: 2, Span: {1, 2, 4} , Primitive root: False Element: 3, Span: {1, 2, 3, 4, 5, 6}, Primitive root: True Element: 4, Span: {1, 2, 4} , Primitive root: False Element: 5, Span: {1, 2, 3, 4, 5, 6}, Primitive root: True Element: 6, Span: {1, 6} , Primitive root: False
The algorithm is also efficient for very large \(n\).
In [13]: n = 1000000000000000035000061 In [14]: galois.primitive_root(n) Out[14]: 7 In [15]: galois.primitive_root(n, reverse=True) Out[15]: 1000000000000000035000054
Here is a counterexample with no primitive roots, \(n = 8 = 2^3\), which does not fit the definition of cyclicness, see
galois.is_cyclic
.In [16]: n = 8 In [17]: root = galois.primitive_root(n); root # The congruence class coprime with n In [18]: Znx = set([a for a in range(1, n) if math.gcd(n, a) == 1]); Znx Out[18]: {1, 3, 5, 7} # Euler's totient function counts the "totatives", positive integers coprime with n In [19]: phi = galois.euler_totient(n); phi Out[19]: 4 In [20]: len(Znx) == phi Out[20]: True # Test all elements for being primitive roots. The powers of a primitive span the congruence classes mod n. In [21]: for a in Znx: ....: span = set([pow(a, i, n) for i in range(1, phi + 1)]) ....: primitive_root = span == Znx ....: print("Element: {}, Span: {:<6}, Primitive root: {}".format(a, str(span), primitive_root)) ....: Element: 1, Span: {1} , Primitive root: False Element: 3, Span: {1, 3}, Primitive root: False Element: 5, Span: {1, 5}, Primitive root: False Element: 7, Span: {1, 7}, Primitive root: False # Note the max order of any element is 2, not 4, which is Carmichael's lambda function In [22]: galois.carmichael(n) Out[22]: 2