(Challenge added over 6 years ago.)
Because algebra is fun!
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 :
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 :
- Polynomial Long Division, Wikipedia.
- Polynomial Long Division, Purplemath.
- Dividing Polynomials, West Texas University Intermediate Algebra.
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 :
That is :
- ± - The sign, either
- 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 :
- If the exponent of the term is 1, it won't be included :
- If the exponent of the term is 0, then neither the variable nor the exponent will be included :
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+1If 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 sign, either
- 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.
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.
Many thanks for Flagitious from #codegolf to suggesting and submitting this challenge!