| Bytes | Lang | Time | Link |
|---|---|---|---|
| 111 | PARI/GP | 150430T180230Z | Charles |
| 224 | Mathematica | 160120T095958Z | njpipeor |
PARI/GP, 155 148 135 130 120 111 bytes
This returns the truthy value 1 when a polynomial is irreducible and the special falsy value gnil (which is treated as 0 when you try to use it) when the polynomial is reducible.
(P,d=poldegree(P),B=3^d*norml2(P)^2,t=B*2+1)->for(i=0,t^d,Q=Pol([n-B|n<-digits(i,t)])&&i%t-B&&P%Q==0&&return);1
Test case
%(x^2+x+1)
returns 1 (true). The other examples work similarly.
Note that this is extremely slow. An irreducible polynomial of degree d with coefficients between -m and m will require constructing around 3d2 * (d+1)d * m4d potential polynomial factors. Reducible polynomials are a bit faster.
PARI/GP, 16 bytes, cheap as hell
Technically not disallowed (noting that the command doesn't factor or equation-solve):
polisirreducible
Ungolfed version of #1
This version checks all possible factors up to the Beauzamy bound, so it's very slow. It's still faster than the golfed version, which checks further to save characters.
Beauzamy(P)=
{
my(d=poldegree(P),s,c);
s=sum(i=0,d,polcoeff(P,i)^2/binomial(d,i));
c = 3^(3/2 + d);
c *= s / (4*d*Pi);
abs(c * pollead(P))
}
factorpol(P)=
{
my(B=Beauzamy(P)\1, t=B*2+1, d=poldegree(P)\2, Q);
for(i=0,t^(d+1)-1,
Q=Pol(apply(n->n-B, digits(i,t)));
if(Q && poldegree(Q) && P%Q==0, return(Q))
);
0
}
irr(P)=
{
factorpol(P)==0
}
Mathematica, 224 bytes
f@p_:=(e=p~Exponent~x;r=Range[⌈e/-4⌉,(e+2)/4];e<2||FreeQ[PolynomialRemainder[p,Thread@{r,#}~InterpolatingPolynomial~x,x]&/@Tuples[#~Join~-#&[Join@@Position[#/Range@Abs@#,_Integer]]&/@#]~DeleteCases~{(a_)..},0|{}]&[p/.x->r])
Explanation:
Kronecker's method is used here. This method generates certain lower degree polynomials and tests whether there exists a factor of the original polynomial.
Test cases:
f/@{x+3, -2x, x^2+x+1, x^3-3x-1, -2x^6-3x^4+2, 3x^9-8x^8-3x^7+2x^3-10}
(* {True, True, True, True, True, True} *)
f/@{x^2, x^2+2x+1, x^4+2x^3+3x^2+2x+1, -3x^7+5x^6-2x, x^9-8x^8+7x^7+19x^6-10x^5-35x^4-14x^3+36x^2+16x-12}
(* {False, False, False, False, False} *)
It takes 14s on my laptop to conclude that 3x^9-8x^8-3x^7+2x^3-10 is prime.