@@ -976,28 +976,36 @@ def q_stirling_number2(n, k, q=None):
976
976
q_int (k , q = q ) * q_stirling_number2 (n - 1 , k , q = q ))
977
977
978
978
979
- def number_of_irreducible_polynomials (q , n ):
979
+ def number_of_irreducible_polynomials (n , q = None ):
980
980
r"""
981
- Return the number of irreducible polynomials of degree ``n``
982
- over the finite field with ``q`` elements.
981
+ Return the number of monic irreducible polynomials of degree ``n``
982
+ over the finite field with ``q`` elements. If ``q`` is not given,
983
+ the result is returned as a polynomial in `\QQ[q]`.
983
984
984
985
INPUT:
985
986
986
- - ``q`` -- prime power
987
987
- ``n`` -- positive integer
988
+ - ``q`` -- ``None`` (default) or a prime power
988
989
989
990
OUTPUT: integer
990
991
991
992
EXAMPLES::
992
993
993
- sage: number_of_irreducible_polynomials(2, 8 )
994
+ sage: number_of_irreducible_polynomials(8, q=2 )
994
995
30
995
- sage: number_of_irreducible_polynomials(9, 9)
996
+ sage: number_of_irreducible_polynomials(9, q= 9)
996
997
43046640
997
998
999
+ ::
1000
+
1001
+ sage: poly = number_of_irreducible_polynomials(12); poly
1002
+ 1/12*q^12 - 1/12*q^6 - 1/12*q^4 + 1/12*q^2
1003
+ sage: poly(5) == number_of_irreducible_polynomials(12, q=5)
1004
+ True
1005
+
998
1006
This function is *much* faster than enumerating the polynomials::
999
1007
1000
- sage: num = number_of_irreducible_polynomials(101, 99 )
1008
+ sage: num = number_of_irreducible_polynomials(99, q=101 )
1001
1009
sage: num.bit_length()
1002
1010
653
1003
1011
@@ -1006,9 +1014,14 @@ def number_of_irreducible_polynomials(q, n):
1006
1014
Classical formula `\frac1n \sum_{d\mid n} \mu(n/d) q^d` using the
1007
1015
Möbius function `\mu`; see :func:`moebius`.
1008
1016
"""
1017
+ n = ZZ (n )
1018
+ if n <= 0 :
1019
+ raise ValueError ('n must be positive' )
1020
+ if q is None :
1021
+ q = ZZ ['q' ].gen ()
1022
+ R = parent (q )
1009
1023
from sage .arith .misc import moebius
1010
- q , n = ZZ (q ), ZZ (n )
1011
- r = ZZ .zero ()
1012
- for d in n .divisors ():
1013
- r += moebius (n // d ) * q ** d
1014
- return r // n
1024
+ r = sum ((moebius (n // d ) * q ** d for d in ZZ (n ).divisors ()), R .zero ())
1025
+ if R is ZZ :
1026
+ return r // n
1027
+ return r / n
0 commit comments