Factoring Polynomials

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Dec 18 20:10:28 EST 2008


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



More information about the Python-list mailing list