Back
Close

FOIL Method

Statement

 Goal

FOIL is the standard method for multiplying two binomials together. The word FOIL is an acronym for the four terms of the product:

First: the "first" terms of each binomial are multiplied together.
Outer: the "outside" terms are multiplied—that is, the first term of the first binomial and the second term of the second.
Inner: the "inside" terms are multiplied—that is, the second term of the first binomial and the first term of the second.
Last: the "last" terms of each binomial are multiplied.

In general: (ax + b)(cx + d) = acx² + adx + bcx + bd = acx² + (ad+bc)x + bd.

Your objective is to print the simplified expansion for the given binomial product.
Input
Line 1: The Coefficient of the left binomial, the Constant of the left binomial, the Coefficient of the right binomial, the Constant of the right binomial.

Example:
1 3 1 2

This refers to the product
(x + 3)(x + 2)
Output
A line containing the expansion of a product in the form ax^2 ± bx ± c where a and b are Coefficients, and c is a Constant.

Example:
x^2 + 5x + 6
Note:
If a Coefficient is 0, do not include the term. Do not use 0x.
If a Coefficient is 1, do not include it. Use x not 1x.
If the leading Coefficient is negative, do not add a space between the term and the negative sign. Use -3x^2 not - 3x^2.
If a non-leading Coefficient or a Constant is negative, do not add the negative term, subtract the positive term. Do x^2 - 14x - 351 not x^2 + -14x + -351.
Constraints
Coefficient is an integer that cannot be 0
Constant is an integer that cannot be 0
Example
Input
1 3 1 2
Output
x^2 + 5x + 6

Game modes
Fastest, Shortest, Reverse

Test cases
Single-Digit Constants Test
Input
1 3 1 2
Output
x^2 + 5x + 6

Single-Digit Constants Validator Validator
Input
1 2 1 4
Output
x^2 + 6x + 8

Negative Constants Test
Input
1 1 1 -9
Output
x^2 - 8x - 9

Negative Constants Validator Validator
Input
1 3 1 -1
Output
x^2 + 2x - 3

Multi-Digit Constants Test
Input
1 13 1 -27
Output
x^2 - 14x - 351

Multi-Digit Constants Validator Validator
Input
1 -21 1 30
Output
x^2 + 9x - 630

Coefficients Test
Input
3 2 6 9
Output
18x^2 + 39x + 18

Coefficients Validator Validator
Input
4 1 3 9
Output
12x^2 + 39x + 9

Negative Coefficients Test
Input
-1 7 3 4
Output
-3x^2 + 17x + 28

Negative Coefficients Validator Validator
Input
-7 1 -6 2
Output
42x^2 - 20x + 2

Multi-Digit Coefficients Test
Input
11 -33 25 72
Output
275x^2 - 33x - 2376

Multi-Digit Coefficients Validator Validator
Input
21 -37 45 69
Output
945x^2 - 216x - 2553

Difference of Two Squares Test
Input
1 3 1 -3
Output
x^2 - 9

Difference of Two Squares Validator Validator
Input
1 1 1 -1
Output
x^2 - 1

Solution language

Solution

Stub generator input