Quadratic factoriser

Apr 8, 2012 at 6:37pm
Does anyone think it's possible to create a program to factorise a quadratic equation?

How difficult 1-10?
Apr 8, 2012 at 6:59pm
It would probably require a good amount of effort, you would have to do a lot of advanced string manipulation among other headache provoking things.

I would rate it a 8 or so, you would need to have some pretty good skills and algorithms ;)
Apr 8, 2012 at 7:11pm
i would like to say it is a 3. using numbers in programming is a lot easier than on paper. remember the quadratic formula? that works perfectly for this. and no string is needed if you are only do a formula like
ax^2 + bx + c
just request a b and c.
and your formula is

x = [ -b ± sqrt(b^2 - 4ac) ] / 2a

so for a computer do

const float answer = ((0 - b) + sqrt((b*b) - (4*a*c))) / (2*a);
const float second_answer = ((0 - b) - sqrt((b*b) - (4*a*c))) / (2*a);

you can also include an evaluation for the discriminate by doing a 2 or 3 if statements.
no need for algorithms. no need for strings. no headache.
i did a similar thing for my calc class. helped so much for understanding this and homework took no time at all.
Last edited on Apr 8, 2012 at 7:15pm
Apr 8, 2012 at 7:26pm
Factorising, not solving. Solving is easy!
Apr 8, 2012 at 7:35pm
write it on paper. following the change of variables. maybe a 4.
so lets work backwards.
we have (x+5)(x+3) = 0

lets multiply
x^2 + 8x + 15 so what happened?

we need a few variables here. so lets say its (ax+b)(cx+d)

((ax*cx) + (((a*d)+(c*b))*x) + ( b*d))

so now that we know what happened in this formula we can work forward.
we start with our answer

x^2 + 8x + 15

ax^2 + bx + c
lets find the factors
1
2
3
4
5
6
7
  std::vector<int> factors; 
    for(int i = 0; i != a*c; i--){//evaluate if a*c is negative 
        for(int j = 0; j < 100; j++){ 
            if(j * i == a * c) { factors.push_back(j); factors.push_back(i);} 
           //factors come in pairs 
    }
    }


1
2
3
4
5
6
7
   std::vector<int> factors; 
    for(int i = 0; i != a*c; i++){//evaluate if a*c is positive
        for(int j = 0; j < 100; j++){ 
            if(j * i == a * c) { factors.push_back(j); factors.push_back(i);} 
            //factors come in pairs
    }
    }

next, what you need to do is find a way to solve for a.

if you still need help post a question. but that set you off to a good start. but i do not want to write it for you.

Last edited on Apr 8, 2012 at 8:43pm
Apr 8, 2012 at 8:42pm
If you wanted it to be factored from an entered equation as a string then it would be heck of a lot more difficult. But yeah I'm not the best person for math, I would take ui uiho's advice ;)
Apr 17, 2012 at 2:41am
I think it is pretty easy.

You could simply solve it using the Quadratic Formula. Once you know the roots (root1 and root2), you could then put the equation into the form (x - root1)(x - root2).

The coding part is pretty easy.
Just some input statements, output statements, a couple if-conditions, . . .
Nothing really sophisticated.

I think what trips up most people is simply not being thorough about the formula in the first place. Especially people who are new to coding; they don't want to bother checking first if a equals 0, if the discriminant is 0 or less than 0, etc. They just want to write a short program that assumes all results will be nice real numbers.

On a scale of 1 to 10, I'd rate this task a 1, very easy.
Apr 17, 2012 at 3:11am
you forgot the chance there is an equation like
14x^2+12x+213 its better to solve it using 3 agorithms. one of which i already posted.
Topic archived. No new replies allowed.