Prime polynomials

21

4

Given a polynomial, determine whether it's prime.

A polynomial is ax^n + bx^(n-1) + ... + dx^3 + ex^2 + fx + g, where each term is a constant number (the coefficient) multiplied by a nonnegative integer power of x. The highest power with a nonzero coefficient is called the degree. For this challenge, we only consider polynomials of at least degree 1. That is, each polynomial contains some x. Also, we only use polynomials with integer coefficients.

Polynomials can be multiplied. For example, (x+3)(2x^2-2x+3) equals 2x^3+4x^2-3x+9. Thus, 2x^3+4x^2-3x+9 can be factored into x+3 and 2x^2-2x+3, so it is composite.

Other polynomials can not be factored. For example, 2x^2-2x+3 is not the product of any two polynomials (ignoring constant polynomials or those with non-integer coefficients). Hence, it is prime (also known as irreducible).

Rules

  • Input and output can be through any standard way.
  • Input can be a string like 2x^2-2x+3, a list of coeffecients like {2,-2,3}, or any similar means.
  • Output is either a truthy value if it's prime, or a falsey value if it's composite. You must yield the same truthy value for all primes, and the same falsey value for all composite polynomials.
  • The input will be of at least degree 1 and at most degree 10.
  • You may not use built-in tools for factorization (of integers or expressions) or equation solving.

Examples

True - prime

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

False - composite

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

Ypnypn

Posted 2015-03-20T17:42:59.853

Reputation: 10 485

11From some quick googling this is a hard problem regardless of golfing. – orlp – 2015-03-20T18:15:01.947

So, given a list of 3 coefficients, we determine whether or not it can be factored? – kirbyfan64sos – 2015-03-20T18:17:53.410

@kirbyfan64sos given a list of all coeficients of a polynomial of degree from one to ten (inclusive), so anywhere between two to eleven coeficients.

I personally think that up to ten is silly if we are talking about a golfing problem. I suggest degree one to degree five for a moderate dificulity but still golfable approach. – cirpis – 2015-03-20T18:29:50.260

5

Am I correct in thinking that by prime you mean irreducible? If so then this is basically a variant on this question about factoring polynomials, and I suspect that it won't attract any answers which don't factor.

– Peter Taylor – 2015-03-20T19:20:36.680

@PeterTaylor Correct. Is there really no easier way? – Ypnypn – 2015-03-20T19:29:38.697

1

According to this recent paper, "We are interested in the question of deciding whether a given polynomial is irreducible or not. Consequently, a simple test or criterion which would give this information is desirable. Unfortunately, no such criterion which will apply to all the classes of polynomials has yet been devised".

– Peter Taylor – 2015-03-20T19:43:00.543

cough Rational Root Theorem cough Edit: this won't actually solve the problem if the polynomial is divisible, by, say, x^2 + x + 1 and another irreducible quadratic. – BrainSteel – 2015-03-20T19:56:49.540

2@AlexA., there are many many "if" tests which work for some polynomials, but the question is asking for an "if and only if" test which works for all polynomials. – Peter Taylor – 2015-03-20T20:07:19.637

1This is a nice problem! Note that usually polynomials are only prime relative to a base ring (or field). In particular, if the field is the complex numbers, then no polynomial of degree greater than 2 is prime. So I would specify whether you want Rational (probably the most straightforward) Integer (this will involve some integer factoring as well), or modulo some number m. If m is prime, then there are rather easy algorithms. Otherwise things are a bit more tricky...(but feasible) – cody – 2015-03-20T20:46:48.267

@cody OP specified integers. – orlp – 2015-03-20T21:17:21.863

Shouldn't input be something along the lines of {[2, 2], [3, 1], [a, b]} as in ax^b? – ASCIIThenANSI – 2015-04-06T20:54:23.643

@ASCIIThenANSI You can accept input like that, if you want. – Ypnypn – 2015-04-12T16:26:51.233

We are testing irreducibility over what field/ring? The integers? The rationals? The reals? The complex numbers? If it's one of the latter two, it's rather trivial due to the Fundamental Theorem of Algebra, so I'd assume either the integers or the rationals, but it's crucial that you specify which. – Justin – 2015-11-04T16:18:28.753

1@Justin It's the integers: "ignoring constant polynomials or those with non-integer coefficients". – lirtosiast – 2015-11-08T04:56:28.727

If not is allowed to use 'solve' for factoring polynomial my solution is out. factor can not be used on polynomial, factor can be used on integers? Thanks – RosLuP – 2016-11-14T20:33:38.320

Answers

3

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}
(* {True, True, True, True, True} *)

It takes 14s on my laptop to conclude that 3x^9-8x^8-3x^7+2x^3-10 is prime.

njpipeorgan

Posted 2015-03-20T17:42:59.853

Reputation: 2 992

1

PARI/GP, 16 bytes, cheap as hell

For some reason this wasn't disallowed (noting that the command doesn't factor or equation-solve):

polisirreducible

Test case

%(x^2+x+1)

returns 1 (true). The other examples work similarly.

But to show that this is solvable the hard way, here's a full solution.

Less cheap, but sloooooooooow

There's really no point golfing this.

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
}

Edit: Commenters have pointed out that the first method may be disallowed by good taste, the spirit of the rules, the Geneva Convention, standard loophole rules, etc. I don't agree, but in any case I posted the second version along with the first and certainly it seems acceptable.

Charles

Posted 2015-03-20T17:42:59.853

Reputation: 2 435

1Hmmmm... I'm pretty sure that this command does factor and/or solve equations under the hood. (Also, if a challenge disallows certain built-ins is kinda implied that a built-in that just solves the problem is also not in the spirit of the challenge.) – Martin Ender – 2015-04-30T18:05:09.603

@MartinBüttner: I think that the first answer fits the letter, but not the spirit, of the challenge's rules. That's why I wrote the second version, which is a legitimate solution. It can check that x^4+1 (which is famously reducible mod any prime) is irreducible in 86 milliseconds. If nothing else others can adapt and golf this version. – Charles – 2015-04-30T18:07:52.547

1

The first answer falls in a loophole which is banned by default: Using built-in functions to do the work. Please remove it from your answer, or at least indicate that it is not a valid solution.

– isaacg – 2015-04-30T18:17:36.983

5@isaacg That is not currently a valid standard loophole (due to the vote breakdown +44/-29). Charles, if you agree that only the second answer is really legitimate, then you should include its byte count instead. – Martin Ender – 2015-04-30T18:22:12.410

@MartinBüttner: I don't -- I think both are legitimate by the rules of this question and the general loopholes thread. But I added a comment to point out the issue. – Charles – 2015-04-30T18:27:33.943

-1. The second answer (which is arguably the only valid one) is not golfed and doesn't have its byte count listed. You may not use built-in tools for factorization (of integers or expressions) or equation solving. – mbomb007 – 2016-11-14T21:16:33.340

@mbomb007 If the original question poser edits to clarify that polisirreducible is disallowed, I will remove the first version and golf the second. – Charles – 2016-11-15T01:05:02.677

polisirreducible uses factoring in its implementation, and is therefore disallowed. According to a code search, its C-Name is gisirreducible. The definition of that function can be seen here (Ctrl+F, search for gisirreducible). You can see that the definition of the function contains a call to factor. The definition for that can also be seen, and it does factor polynomials. – mbomb007 – 2016-11-15T15:54:43.370

@MartinEnder The comment above shows that you are correct. It factors polynomials under the hood. – mbomb007 – 2016-11-15T16:00:35.343