Factoring Polynomials Program

I can sort of get the answer but it does not function properly. I would appreciate any help. Here is the root question:

Example 6-16 shows how to write a program to factor a polynomial of the form x² + bx + c, where b and c are integers. Modify the program so that it can also factor polynomials of the form ax2 + bx + c , where a, b, and c are integers. Note that the polynomial _ - 2x² - 3x + 2_ can be factored as: _ - 2x² - 3x + 2 = (-2x + 1)(x + 2) = - (2x - 1)(x + 2)_.

Here is what I have thus far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cmath>
using namespace std;
void factorization(int b, int c, int& u1, int& v1, bool& isFactorable);
int main() {
    // Write your main here
    int coeffofX;
    int constantTerm;
    int u;
    int v;
    bool isPolynomialFactorable;

    cout << "Enter the coefficient of x: ";
    cin >> coeffofX;
    cout << endl;

    cout << "Enter the constant term: ";
    cin >> constantTerm;
    cout << endl;

    factorization(coeffofX, constantTerm, u, v, isPolynomialFactorable);
    if (isPolynomialFactorable)
    {
      cout << "x^2";
      if (coeffofX > 0)
       cout << "+" << coeffofX << "x";
      else if (coeffofX < 0)
       cout << "-" << coeffofX << "x";

      cout << " = (x";
      if (u > 0)
       cout << "-" << u << ")(x";
      else if (u < 0)
       cout << "+" << u << ")(X";
      if (v > 0)
       cout << "-" << v << ")" << endl;
      else if (v < 0)
       cout << "+" << abs(v) << ")" << endl;

    }
    else
      cout << "The polynomial is not factorable." << endl;
    return 0;
}
void factorization(int b, int c, int& u1, int& v1, bool& isFactorable){
  double discriminant;
  int temp;
  isFactorable = true;
  discriminant = b * b - 4 * c;

  if (discriminant < 0)
   isFactorable = false;
   else
   {
    temp = static_cast<int>(sqrt(discriminant));

    if (temp * temp != discriminant)
      isFactorable = false;
    else
    {
      if (((-b + temp) % 2 != 0) || ((-b - temp) % 2 != 0))
       isFactorable = false;
      else
      {
        u1 = (-b + temp) / 2;
        v1 = (-b - temp) / 2;
      }
    }
  } 
}
Last edited on
Do you know the full formula ?
https://www.mathpapa.com/quadratic-formula/

Your code does this, but you're just assuming that a = 1 all the time.

So start with
1
2
3
4
5
6
void factorization(int a, int b, int c, int& u1, int& v1, bool& isFactorable);
...
factorization(1,coeffofX, constantTerm, u, v, isPolynomialFactorable);
...
void factorization(int a, int b, int c, int& u1, int& v1, bool& isFactorable){
...

and fix your implementation to make use of the a parameter as described by the formula.

Once you're happy it still works, then you can update main with
1
2
3
4
int coeffofXsquared;
...
factorization(coeffofXsquared,coeffofX, constantTerm, u, v, isPolynomialFactorable);

Last edited on
I implemented in your code and I am able to provide the correct formula in some cases, but it is not truly finding what is factorable and what is not. Also, it does not correctly output the factoring which is the entire reason for the program.

I enter in: -2, -3, 2

It ouputs: -2x^2 -3x +2 = (x-2)(x-1)

I enter in: 1, 1, 1 or 2, 3, -2

It outputs: The polynomial is not factorable
Which is incorrect of course.
Here is my program according to your changes specified:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cmath>
using namespace std;
void factorization(int a, int b, int c, int& u1, int& v1, bool& isFactorable);
int main() {
    // Write your main here
    int coeffofXsquared;
    int coeffofX;
    int constantTerm;
    int u;
    int v;
    bool isPolynomialFactorable;
    
    cout << "Enter the coefficient of x^2: ";
    cin >> coeffofXsquared;
    cout << endl;

    cout << "Enter the coefficient of x: ";
    cin >> coeffofX;
    cout << endl;

    cout << "Enter the constant term: ";
    cin >> constantTerm;
    cout << endl;

    factorization(coeffofXsquared, coeffofX, constantTerm, u, v, isPolynomialFactorable);
    if (isPolynomialFactorable)
    {
      if(coeffofXsquared > 0)
      cout << coeffofXsquared << "x^2";
      else if(coeffofXsquared < 0)
      cout << "-" << abs(coeffofXsquared) << "x^2";
      
      if (coeffofX > 0)
      cout << "+" << coeffofX << "x";
      else if (coeffofX < 0)
      cout << "-" << abs(coeffofX) << "x";

      if(constantTerm > 0)
      cout << "+" << constantTerm;
      else if (constantTerm < 0)
      cout << "-" << abs(constantTerm);

      cout << " = (x";
      if (u > 0)
       cout << "-" << u << ")(x";
      else if (u < 0)
       cout << "+" << u << ")(X";
      if (v > 0)
       cout << "-" << v << ")" << endl;
      else if (v < 0)
       cout << "+" << abs(v) << ")" << endl;

    }
    else
      cout << "The polynomial is not factorable." << endl;
    return 0;
}
void factorization(int a, int b, int c, int& u1, int& v1, bool& isFactorable){
  double discriminant;
  int temp;
  isFactorable = true;
  discriminant = b * b - 4 * c;

  if (discriminant < 0)
   isFactorable = false;
   else
   {
    temp = static_cast<int>(sqrt(discriminant));

    if (temp * temp != discriminant)
      isFactorable = false;
    else
    {
      if (((-b + temp) % 2 != 0) || ((-b - temp) % 2 != 0))
       isFactorable = false;
      else
      {
        u1 = (-b + temp) / 2;
        v1 = (-b - temp) / 2;
      }
    }
  } 
}
Deer Of School wrote:
I enter in: 1, 1, 1 or 2, 3, -2

It outputs: The polynomial is not factorable
Which is incorrect of course.


Actually, it is perfectly correct. Neither of those polynomials are factorisable (in reals). The first has discriminant -3 and the second has discriminant -7. Negative numbers don't have (real) square roots.



discriminant = b * b - 4 * c;
You are still ignoring @salem c's reply. You are omitting the non-unit value of a. Should be
discriminant = b * b - 4 * a * c;


Also, there's factorisation and there's factorisation: the expression x2-2 is factorisable, but not as rationals. If you want rational factors then the discriminant will not only have to be positive or zero, but would need to be a perfect square as well.
Last edited on
Topic archived. No new replies allowed.