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 [478]: n = 6 In [479]: root = galois.primitive_root(n); root Out[479]: 5 # The congruence class coprime with n In [480]: Znx = set([a for a in range(1, n) if math.gcd(n, a) == 1]); Znx Out[480]: {1, 5} # Euler's totient function counts the "totatives", positive integers coprime with n In [481]: phi = galois.euler_totient(n); phi Out[481]: 2 In [482]: len(Znx) == phi Out[482]: True # The primitive roots are the elements in Znx that multiplicatively generate the group In [483]: 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 [484]: n = 7 In [485]: root = galois.primitive_root(n); root Out[485]: 3 # The congruence class coprime with n In [486]: Znx = set([a for a in range(1, n) if math.gcd(n, a) == 1]); Znx Out[486]: {1, 2, 3, 4, 5, 6} # Euler's totient function counts the "totatives", positive integers coprime with n In [487]: phi = galois.euler_totient(n); phi Out[487]: 6 In [488]: len(Znx) == phi Out[488]: True # The primitive roots are the elements in Znx that multiplicatively generate the group In [489]: 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 [490]: n = 1000000000000000035000061 In [491]: galois.primitive_root(n) Out[491]: 7 In [492]: galois.primitive_root(n, reverse=True) Out[492]: 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 [493]: n = 8 In [494]: root = galois.primitive_root(n); root # The congruence class coprime with n In [495]: Znx = set([a for a in range(1, n) if math.gcd(n, a) == 1]); Znx Out[495]: {1, 3, 5, 7} # Euler's totient function counts the "totatives", positive integers coprime with n In [496]: phi = galois.euler_totient(n); phi Out[496]: 4 In [497]: len(Znx) == phi Out[497]: True # Test all elements for being primitive roots. The powers of a primitive span the congruence classes mod n. In [498]: 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 [499]: galois.carmichael(n) Out[499]: 2