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 leaderboard.)

Rank User Size Language Score [?]
1st flagitious 138 Ruby 10,000 (v4)
2nd shinh 142 Ruby 9,718 (v21)
3rd robin 156 Ruby 8,846 (v21)
4th ySas 158 Perl 8,734 (v38)
5th irori 169 Ruby 8,165 (v10)
6th Olathe 175 Ruby 7,885 (v12)
7th lifthrasiir 178 Python 7,752 (v13)
8th Mark Byers 179 Python 7,709 (v11)
9th hiro.suzuki 181 Python 7,624 (v10)
10th mick 181 Python 7,624 (v30)

See who is active in this challenge →

Polynomial Division

(Challenge added 563 days ago.)

Because algebra is fun!

Discuss This Challenge →

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 :

More Information

  • 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
      instead of
      +1*x^3
    • If the exponent of the term is 1, it won't be included :
      +2*x
      instead of
      +2*x^1
    • If the exponent of the term is 0, then neither the variable nor the exponent will be included :
      +7
      instead of
      +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!