Submit an Entry

To enter the challenge, you need to signup or login.

This Challenge's Best Entries [View All]

(View the Overall | Perl | PHP | Python | Ruby

Rank User Size Language Score [?]
1st dmd 133 Ruby 10,000 (v5)
2nd flagitious 138 Ruby 9,637 (v4)
3rd shinh 142 Ruby 9,366 (v21)
4th ySas 152 Perl 8,750 (v42)
5th robin 156 Ruby 8,525 (v21)
6th primo 166 Perl 8,012 (v27)
7th irori 169 Ruby 7,869 (v10)
8th hallvabo 169 Python 7,869 (v18)
9th Olathe 175 Ruby 7,599 (v12)
10th tryeng 176 Python 7,556 (v24)

See who is active in this challenge →

Polynomial Division

(Challenge added over 6 years ago.)

Because algebra is fun!

The Problem

A polynomial is an expression in which a finite number of constants and variables are combined using only addition, subtraction, multiplication, and non-negative whole number exponents (raising to a power).

In this challenge, we'll be using just one variable which will always be `x`. Examples of polynomials using one variable are :

`x2+x+4`
and
`x11-27x9+12x3-1`

Your code will take two such polynomials, and divide one by the other, printing the result.

There are a number of methods to perform division with polynomials. We recommend reading these links :

• Your code will be given two polynomials on stdin. It will then divide the first (the numerator) by the second (the denominator) and print the resulting polynomial (the quotient) to stdout.

• The input your code will receive will be on three lines :

``` Numerator
-----------
Denominator```

Each line may have leading spaces intended to pad the polynomials so the shorter one is centered with respect to the longer one. Each line ends with a newline character.

• The denominator multipied by the quotient will equal the numerator exactly. You don't need to worry about remainders.

• Each polynomial will contain one or more individual terms. Each term will be formatted as follows :

`±a*x^b`

That is :

• ± - The sign, either `+` or `-`.
• a - The coefficient of the term. The numerator will have coefficients in the range -1,000 to 1,000 inclusive. The denominator (and quotient, for output) will have coefficients in the range -20 to 20 inclusive.
• b - The exponent of the term. The numerator will have exponents in the range 0 to 100 inclusive. The denominator (and quotient, for output) will have exponents in the range 0 to 50 inclusive.

Some additional rules govern the formatting of each term :

• If the coefficient is 1, then it won't be included. That is :
`+x^3`
`+1*x^3`
• If the exponent of the term is 1, it won't be included :
`+2*x`
`+2*x^1`
• If the exponent of the term is 0, then neither the variable nor the exponent will be included :
`+7`
`+7*x^0`

The terms will be combined by placing them immediately after each other without any spaces, with the exponents of the terms ordered from high to low :

`3*x^11+11*x^3-x+1`
If the first term in the polynomial is positive, then the leading `+` will be omitted, as in the example above.

Therefore, here are some examples of valid input. See the 'Examples' section below for more.

```x^3-12*x^2+81
-------------
x-3```
```   x^4-1
-----------
x^3-x^2+x-1```
```-180*x^70+144*x^67-380*x^65+304*x^62-54*x^54-114*x^49
-----------------------------------------------------
-20*x^26+16*x^23-6*x^10```
• The result of the division (the quotient) should obey the same rules as above with regards to formatting the individual terms and omitting the leading `+`. Any trailing whitespace in your output will be ignored.
• Your code will be run eight times. You will need to print the correct result in all eight tests for it to be deemed successful.

Examples

Your code will be run eight times. The input for the first five runs will always be the same. Click on the links below to see the input and the expected output.

The input your code will receive for the sixth, seventh and eighth run will differ on each submission. See an example of this input. Reload the page to get more, random inputs.

Thanks

Many thanks for Flagitious from #codegolf to suggesting and submitting this challenge!