I'm trying to develop a program that will factor a trinomial with a single variable. I'm using a string array and substrings to get the terms, however I'm having trouble with getting the factors of the AC Method. For those who don't know how to factor polynomials using the AC Method below is an example.

A + B + C
8x^2 + 7x + 1 :: Multiply the first term by the third (do not include variables).
8 * 1 = 8; :: Factors of 8 are 1, 2, 4, and 8.

// Now we need to factors that will equal the second term.

1 :: 8
2 :: 4

8 - 1 = 7; This is our solution.

// Now we re-write the polynomial with four terms to set up for factoring by grouping.

8x^2 + 8x - 1x + 1 :: This is the new polynomial.

// We have to factor by grouping.

8x(x + 1) - 1(x + 1); Becase 8x is common between the first two terms, and x + 1 is what's left after pulling 8x out of 8x^2 + 8x. Same for the second half; 1 is common and x + 1 is what's left after pulling 1 out of both terms.

So the result is (8x - 1)(x + 1).

I have figured out the disection of the polynomial in a generic way using a for loop. What I want to learn is how to get the factors of TermA * TermC.

Here is my disection so you know how I'm getting the terms for factoring.

private string[] Poly1 = new string[1000];
private string[] Poly2 = new string[1000];

private int TermA, TermB, TermC, TermD;

private void DisectString(string StringToDisect)
{
for (int i = 0; i < StringToDisect.Length; i++)
{
Poly1[i] = StringToDisect.Substring(i, 1);
}

AssignTerms();
}

private void AssignTerms()
{
Term1 = Convert.ToInt32(Poly1[0]);
Term2 = Convert.ToInt32(Poly1[7]);
Term3 = Convert.ToInt32(Poly1[12]);
}

Any help is greatly appreciated on this topic. I'll check back to answer any questions on the topic that are needed. Also, if you post a source code please either make sure it is very well commented, or explain it in detail so that I understand it and can implement it correctly.

xXTaCoXx

First of all the solution of the example is not right as you solved (8x^2 + 7x - 1) not (8x^2 + 7x + 1) you need to read about Discriminant

public List<int> Factors(int n) {
    List<int> result = new List<int>();
    for (int i = 1; i <= n/2; i++) {
        if (n % i == 0) {
            result.Add(i);
            result.Add(n/i);
        }
    }

    return result;
}

If you need them in pairs

public List<FactorPair> Factors(int n) {
    List<FactorPair> result = new List<FactorPair>();
    for (int i = 1; i <= n/2; i++) {
        if (n % i == 0) {
            result.Add(new FactorPair(i, i/n));
        }
    }
    return result;
}

public class FactorPair {
    public int A {get; private set;}
    public int B {get; private set;}

    public FactorPair(int a, int b) {
        this.A = a;
        this.B = b;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.