Factoring Polynomials
Collin D
collin.day.0 at gmail.com
Fri Dec 19 02:42:28 CET 2008
On Dec 18, 5:10 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 18 Dec 2008 11:37:35 -0800, collin.day.0 wrote:
> > I am trying to write a simple application to factor polynomials. I wrote
> > (simple) raw_input lines to collect the a, b, and c values from the
> > user, but I dont know how to implement the quadratic equation
>
> > x = (-b +or- (b^2 - 4ac)^1/2) / 2a
>
> > into python. Any ideas?
>
> def quadratic_solution(a, b, c):
> sol1 = (-b + (b**2 - 4*a*c)**0.5)/2*a
> sol2 = (-b - (b**2 - 4*a*c)**0.5)/2*a
> return (sol1, sol2)
>
> Because this looks like homework, I've deliberately left in two errors in
> the above. One of them is duplicated in the two lines above the return,
> and you must fix it or you'll get radically wrong answers.
>
> The second is more subtle, and quite frankly if this is homework you
> could probably leave it in and probably not even lose marks. You will
> need to do significant research into numerical methods to learn what it
> is, but you will then get significantly more accurate results.
>
> --
> Steven
The corrected function is:
def quadratic_solution(a,b,c)
sol1 = -1*b + ((b**2 - 4*a*c)**.5)/2*a
sol1 = -1*b - ((b**2 - 4*a*c)**.5)/2*a
return (sol1, sol2)
Squaring the -b would give you some strange solutions.... :D
-CD
More information about the Python-list
mailing list