Pre-Calculus for Programmers

A factoring assistant

A factoring assistant

A homework tool is going to suggest a factorisation of x2+bx+cx^{2}+bx+c. The step it needs help with is the search from the lesson on factoring: find two whole numbers whose sum is b and whose product is c, because those two numbers are what goes inside the brackets.

Given b and c, return the pair as a list of two numbers with the smaller first. If no pair of whole numbers works, return an empty list, which is the tool's way of saying the trinomial does not factor over the whole numbers.

Worked example

Input: b = -5, c = 6

The product 6 is positive and the sum -5 is negative, so both numbers are negative.
Pairs multiplying to 6: (-1, -6) sums to -7,  (-2, -3) sums to -5.

Output: [-3, -2]      because -3 + -2 = -5 and -3 * -2 = 6,
                      and the smaller of the two comes first
The trinomial x2−5x+6x^{2}-5x+6.

The awkward ends

Input: b = 1,  c = 1    Output: []          nothing works
Input: b = -2, c = 1    Output: [-1, -1]    the same number twice is allowed
Input: b = 5,  c = 0    Output: [0, 5]      0 * 5 = 0 and 0 + 5 = 5
No pair, a repeated number, and a product of zero.

Constraints

  • b is from −1000-1000 to 1000 and c is from −10000-10000 to 10000.
  • Return the two numbers in increasing order, so [-3, -2] and never [-2, -3].
  • The two numbers may be equal, and either or both may be zero or negative.
  • There is never more than one such pair, up to order, so there is no choice to make about which to return.
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.