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
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.543cough 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.5402@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 inax^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