I don't think it's a requirement. If you don't like that, you'll hate the result of x2+2x+3.
Also, note the square root. Since there's two results for it, there's actually two possible answers. For your example -2x2-13x-15, the two possible answers are (-2*x-10)*(x+1.5) and (-2*x-3)*(x+5). If you don't like one answer, feel free to try the other.
[Edited by - smart_idiot on July 24, 2005 3:32:54 AM]
Factorising quadratic trinomials where the coefficient of X^2 is not 1
How would i code it to produce both answers?
(Note - VB doesn't have complex numbers!)
From,
Nice coder
(Note - VB doesn't have complex numbers!)
From,
Nice coder
Just calculate it twice, using the positive and negative roots, i.e., change e=b/a/2+sqrt(b*b-(4*a*c))/a/2 to e=b/a/2-sqrt(b*b-(4*a*c))/a/2 for the second time.
Ok, good.
I've got it working now.
But a small problem.
for "-2X^2 + 13X + 25"
The answer it spits back is (-2X + 16.1046863561493)(X + 1.55234317807464)
Which isn't very right.
It does produce the right answer "(-2X - 3)(X + 5)" to your equasion "-2x^2 - 13x - 15" tho.
From,
Nice coder
I've got it working now.
But a small problem.
for "-2X^2 + 13X + 25"
The answer it spits back is (-2X + 16.1046863561493)(X + 1.55234317807464)
Which isn't very right.
It does produce the right answer "(-2X - 3)(X + 5)" to your equasion "-2x^2 - 13x - 15" tho.
From,
Nice coder
Maxima gives me this:
Looks like -2X^2 + 13X + 25 to me.
(%i1) expand((-2*X+16.1046863561493)*(X+1.55234317807464)); 2(%o1) - 2 X + 13.00000000000002 X + 25.0000000000001
Looks like -2X^2 + 13X + 25 to me.
You could tell it to output square root values symbolically rather than actually evaluating them... you might want to make a class like so:
// very not tested!class squareRootFraction { // represents sqrt(num/denom). int num, denom; void reduce() { int div = gcd(num, denom); // todo: implement num /= div; denom /= div; } public: squareRootFraction(int num, int denom = 1) : num(num), denom(denom) { reduce(); } squareRootFraction& operator /= (int x) { denom *= x*x; reduce(); return *this; } squareRootFraction& operator *= (int x) { num *= x*x; reduce(); return *this; } friend ostream& operator<<(ostream& os, const squareRootFraction& me) { os << "sqrt(" << me.num << "/" me.denom << ")"; return os; }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement