Jump to content
  • Advertisement
Sign in to follow this  
Nice Coder

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.

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
Share on other sites

Private Sub Form_Load()
Dim t As Integer
Dim u As Integer
tim = Timer
Open "C:\Q.txt" For Input As #1
Open "C:\A.txt" For Output As #2
Do Until EOF(1)
Input #1, t
Input #1, u

Print #2, "=X^2 " & IIf(t >= 0, "+ ", "- ") & IIf(Abs(t) <> 1, Abs(t), "") & "X " & IIf(u >= 0, "+ ", "- ") & Abs(u)

lim = 10
Do
For x = -lim To lim
For y = -lim To lim
s = x + y
p = x * y
DoEvents
If s = t And p = u Then
Print #2, "=(X " & IIf(x >= 0, "+ ", "- ") & Abs(x) & ")(X " & IIf(y >= 0, "+ ", "- ") & Abs(y) & ")"
GoTo nextnum:
End If
Next y
Next x
lim = lim * 2
Loop
nextnum:
Print #2, ""
Loop
Close #1
Print #2, "Time required: " & Timer - tim
Close #2
End
End 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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Nice Coder
Hmm, 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!