Function: factormodcyclo
Section: number_theoretical
C-Name: factormodcyclo
Prototype: LGD0,L,Dn
Help: factormodcyclo(n,p,{single=0},{v='x}): factor n-th
 cyclotomic polynomial mod p.
Doc: Factors $n$-th cyclotomic polynomial $\Phi_{n}(x)$ mod $p$,
 where $p$ is a prime number not dividing $n$.
 Much faster than \kbd{factormod(polcyclo(n), p)}; the irreducible
 factors should be identical and given in the same order.
 If \var{single} is set, return a single irreducible factor; else (default)
 return all the irreducible factors. Note that repeated calls of this
 function with the \var{single} flag set may return different results because
 the algorithm is probabilistic. Algorithms used are as follows.

 Let $F=\Q(\zeta_{n})$. Let $K$ be the splitting field of $p$ in $F$ and $e$ the
 conductor of $K$. Then $\Phi_{n}(x)$ and $\Phi_{e}(x)$ have the same
 number of irreducible factors mod $p$ and there is a simple algorithm
 constructing irreducible factors of $\Phi_{n}(x)$ from irreducible
 factors of $\Phi_{e}(x)$. So we may assume $n$ is equal to the
 conductor of $K$.
 Let $d$ be the order of $p$ in $(\Z/n\Z)^{\times}$ and $\varphi(n)=df$.
 Then $\Phi_{n}(x)$ has $f$ irreducible factors $g_{i}(x)\;(1\leq i\leq f)$
 of degree $d$ over $\F_{p}$ or $\Z_{p}$.

 \item If $d$ is small, then we factor $g_{i}(x)$ into
 $d$ linear factors $g_{ij}(x)$, $1\leq j\leq d$ in $\F_{q}[x]\;(q=p^{d})$ and
 construct $G_{i}(x)=\prod_{j=1}^{d} g_{ij}(x)\in \F_{q}[x]$.
 Then $G_{i}(x)\in\F_{p}[x]$ and $g_{i}(x)=G_{i}(x)$.

 \item If $f$ is small, then we work in $K$, which is a Galois extension of
 degree $f$ over $\Q$. The Gaussian period
 $\theta_{k}=\text{Tr}_{F/K}(\zeta_{n}^{k})$ is a sum of $k$-th power of roots
 of $g_{i}(x)$ and $K=\Q(\theta_{1})$.

 Now, for each $k$, there is a polynomial $T_{k}(x)\in\Q[x]$ satisfying
 $\theta_{k}=T_{k}(\theta_{1})$ because all $\theta_{k}$ are in $K$. Let
 $T(x)\in\Z[x]$ be the minimal polynomial of $\theta_{1}$ over $\Q$. We get
 $\theta_{1}$ mod $p$ from $T(x)$ and construct $\theta_{1},\cdots,\theta_{d}$
 mod $p$ using $T_{k}(x)$. Finally we recover $g_{i}(x)$ from
 $\theta_{1},\cdots,\theta_{d}$ by Newton's formula.

 \bprog
 ? lift(factormodcyclo(15, 11))
 %1 = [x^2 + 9*x + 4, x^2 + 4*x + 5, x^2 + 3*x + 9, x^2 + 5*x + 3]
 ? factormodcyclo(15, 11, 1) \\ single
 %2 = Mod(1, 11)*x^2 + Mod(5, 11)*x + Mod(3, 11)
 ? z1 = lift(factormod(polcyclo(12345),11311)[,1]);
 time = 32,498 ms.
 ? z2 = factormodcyclo(12345,11311);
 time = 47 ms.
 ? z1 == z2
 %4 = 1
 @eprog
