20
2
Without using any built-in factoring/polynomial functions, factor a polynomial completely into irreducibles over the integers or a finite field.
Input
Your program/function will receive some prime (or zero) number n
as input. The field/ring is the finite field of that order (ie Z/nZ
), or just Z
if n
is 0
. Your program may fail if n
is not 0
or a prime. The polynomial will be in F[x]
.
Your program/function will also receive the polynomial as input.
There is some flexibility in the input, be sure to specify how you intend to receive input. For example, the polynomial could be input as a list of coefficients, or in the form most people expect (ex:50x^3 + x^2
), or some other reasonable form. Or the format of inputting the field/ring could also be different.
Output
Your program/function will output the polynomial factored completely. You may leave multiple roots expanded (ie (x + 1)(x + 1)
instead of (x + 1)^2
). You may remove whitespace between binary operators. You may replace juxtaposition with *
. You may insert whitespace in weird places. You may reorder the factors into whatever order you want. The x
term could just be (x)
. x
can be written as x^1
; however the constant term may not have x^0
. Extraneous +
signs are allowable. You may not have a term with a 0
in front, they must be left out. The leading term of each factor must be positive, negative signs must be outside.
Test cases, your program should be able to produce output for each of these in reasonable time (say, <= 2 hours):
Input: 2, x^3 + x^2 + x + 1
Output: (x + 1)^3
Input: 0, x^3 + x^2 + x + 1
Output: (x + 1)(x^2 + 1)
Input: 0, 6x^4 – 11x^3 + 8x^2 – 33x – 30
Output: (3x + 2)(2x - 5)(x^2 + 3)
Input: 5, x^4 + 4x^3 + 4x^2 + x
Output: x(x + 4)(x + 4)(x + 1)
Input: 0, x^5 + 5x^3 + x^2 + 4x + 1
Output: (x^3 + 4x + 1)(x^2 + 1)
Special thanks to Peter Taylor for critiquing my test cases
1
I think this is giving me a flashback to some of the harder undergraduate maths. Am I even heading in the right direction here?
– Digital Trauma – 2014-10-04T16:40:21.9031This reminds me of the time I had nightmares trying to print polynomials correctly... – Sp3000 – 2014-10-17T07:57:52.063
Sorry that I did not understand, but what is the first input number supposed to do ? or how does it affects the output ? – Optimizer – 2014-10-27T07:27:04.830
@Optimizer The first input number determines what field/the integers you are working over. If the number is nonzero, you are working over the finite field of that order. A finite field of order
p
has the elements{0, 1, ... , p-1}
and it is under addition/multiplication modp
. Basically, reduce any coefficient by modp
and you're good. Also, note that if it has a root, ie linear factor, one of{0, ... , p-1}
will produce0
(modp
) when it is plugged into the polynomial. – Justin – 2014-10-27T07:31:59.023How does
0, 6x^4 – 11x^3 + 8x^2 – 33x – 30
work then ? – Optimizer – 2014-10-27T07:32:50.153@Optimizer That means we are working over the integers, which we are much more familiar with. If it factors, then it is the product of two integer-coefficient polynomials. BTW, I just gave you a brief preview of field-theory, you should definitely go and learn it. – Justin – 2014-10-27T07:34:04.407
You don't understand my question. What I meant was that even without the first input, all the examples' answers won't change at all. Or will they ? – Optimizer – 2014-10-27T08:00:06.733
@Optimizer 1st example vs 2nd example,
– Justin – 2014-10-27T16:03:46.460x^2 + 1
doesn't factor into(x + 1)^2
over the integers, but it does over the finite field of order 2.Could you prehaps restrict it to just the finite fields
Z/nZ
? I just thought factoring polynomials in(Z/nZ)[x]
is quite a bit different from factoring inZ[x]
or am I missing something? And I'd also limit the size of the polynomials, since it can be pretty difficult to factor polynomials of arbitrary degrees. – flawr – 2014-10-27T16:15:11.337@flawr I considered doing that restriction, but Peter Taylor requested that I leave this. And there is an algorithm for factoring over
Z/nZ
that doesn't work overZ
, but there is an algorithm that works forZ
which can also be made to work forZ/nZ
. How would arbitrary degrees make it terribly hard? I think it's fine. – Justin – 2014-10-27T16:20:17.6331@flawr, the standard approach to factoring over
Z
is to factor overZ/pZ
for a suitablep
and then Hensel lift. However, the golfable approach is probably (and this is certainly the route I'm looking at) to use a simple bound on the height of the factors and brute force it. – Peter Taylor – 2014-10-27T16:29:51.810@PeterTaylor Yes that's pushing it a little bit too much. "You may not have a term with a 0 in front". I guess
x^1
and s okay, and1x
is also okay. I'm thinking though, the biggest part of this challenge should be factoring, so I don't want to restrict output that much, so I'll allow the extraneous+
s. – Justin – 2014-10-28T18:30:58.347