# Factorising quadratic trinomials where the coefficient of X^2 is not 1

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, i'm factorising quadratic trinomials, and i want to find a really fast way to do it. (i would also like to know how to do it without using guess and check.) What i want to know, is what is the fastest way to do this? From, Nice coder [Edited by - Nice Coder on July 23, 2005 10:41:21 PM]

##### Share on other sites
If you tell us what method you are using then we could tell you if faster ways exist.

##### Share on other sites
Private Sub Form_Load()Dim t As IntegerDim u As Integertim = TimerOpen "C:\Q.txt" For Input As #1Open "C:\A.txt" For Output As #2Do Until EOF(1)Input #1, tInput #1, uPrint #2, "=X^2 " & IIf(t >= 0, "+ ", "- ") & IIf(Abs(t) <> 1, Abs(t), "") & "X " & IIf(u >= 0, "+ ", "- ") & Abs(u)lim = 10DoFor x = -lim To limFor y = -lim To lims = x + yp = x * yDoEventsIf s = t And p = u Then    Print #2, "=(X " & IIf(x >= 0, "+ ", "- ") & Abs(x) & ")(X " & IIf(y >= 0, "+ ", "- ") & Abs(y) & ")"    GoTo nextnum:End IfNext yNext xlim = lim * 2Loopnextnum:Print #2, ""LoopClose #1Print #2, "Time required: " & Timer - timClose #2EndEnd Sub

Thats my code so far.
Its now currently at 0.0032552083 seconds per node. (Which is fast, but we can do better!)

From,
Nice coder

##### Share on other sites
Your timing method includes time spent on outputting the diagnostic messages. I/O is really slow stuff.

##### Share on other sites
Hmm, yes.

But is there anything faster? (ie. for the algorithm?)

Also, if the coefficient of X^2 is not 1, how would i factorise them?

From,
NIce coder

[Edited by - Nice Coder on July 23, 2005 10:13:08 PM]

##### Share on other sites
Quote:
 Original post by Nice CoderHmm, yes.But is there anything faster? (ie. for the algorithm?)

Actually use the quadratic formula perhaps? (which would be a hell of a lot more general, too)

Quote:
 Also, if the coefficient of X^2 is not 1, how would i factorise them?

Just divide through by said coefficient first (and list it as a separate constant factor in the result); a constant factor will not affect where the roots are.

##### Share on other sites
No promises that I didn't screw any of this up.

The math:
(a*x+f)*(x+e)=a*x*x+b*x+c
e=b/a/2+sqrt(b*b-(4*a*c))/a/2
f=c/e

A program:
#include <iostream>#include <complex>#include <string>#include <sstream>std::string comp(const std::complex<double> &var) {  std::stringstream stream;    if(var.real() && var.imag())   {    stream << "(" << var.real();        if(var.imag() >= 0)     stream << '+' << var.imag() << "i)";    else stream << var.imag() << "i)";   }  else if(var.imag())   {    if(var.imag() >= 0)     stream << var.imag() << "i";    else stream << var.imag() << "i";   }  else if(var.real())   stream << var.real();    return stream.str(); }std::string mult(const std::string &a, const std::string &b) {  if(a.empty())   return a;  else if(a == "1")   return b;  else return a + "*" + b; }std::string add(const std::string &a, const std::string &b) {  if(a.empty())   {    if(b.empty())     return "0";    else return b;   }  else if(b.empty())   return a;  else if(b[0] == '-')   return a+"-"+(b.c_str()+1);  else return a + "+" + b; }int main() {  std::complex<double> a, b, c;    std::cout << "a*x^2+b*x+c\na=";  std::cin >> a;  std::cout << "b=";  std::cin >> b;  std::cout << "c=";  std::cin >> c;    std::complex<double>   e=(0.5*b/a)+(0.5*std::sqrt(b*b-(4.0*a*c))/a),   f=c/e;    // (a*x+f)*(x+e)  std::cout << "("<<add(mult(comp(a),"x"),comp(f))<<")*("<<add("x",comp(e))<<")" << std::endl; }

Sample run:
a*x^2+b*x+c
a=2
b=13
c=15
(2*x+3)*(x+5)

[Edited by - smart_idiot on July 24, 2005 3:58:52 AM]

##### Share on other sites
Many thanks to you smart_idiot!

(i would rate++ you, but i've already rated you fully.)

Um, smart idiot, when i try
a = -2
b = -13
c = -15

It spits back an answer of
(-2X + 1.5)(X - 10)

Yet, for X = 17:
-2 * x ^ 2 - (13 * x) - 15 = -814
(-2 * x + 1.5) * (x - 10) = 227.5

From,
Nice coder

##### Share on other sites
I kind of goofed up my math so double check what I wrote. I think it's correct, now. Hey, I had a disclaimer. [wink]

##### Share on other sites
ok, it works now.

Should it be producing whole numbers? (ie. shouldn't factorisation require all of the numbers to remain integers?)

From,
Nice coder

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011154
• ### Who's Online (See full list)

There are no registered users currently online

×