# Factoring Polynomials

Russ P. Russ.Paielli at gmail.com
Fri Dec 19 03:23:11 CET 2008

```On Dec 18, 6:17 pm, Collin D <collin.da... at gmail.com> wrote:
> On Dec 18, 6:12 pm, Collin D <collin.da... at gmail.com> wrote:
>
>
>
> > On Dec 18, 11:37 am, collin.da... at gmail.com 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?
>
> > I completed the code:
>
> > #import
> > from math import sqrt
>
> > # collect data
> > a = float(raw_input('Type a value: '))
> > b = float(raw_input('Type b value: '))
> > c = float(raw_input('Type c value: '))
>
> > # create solver
> > def solver(a,b,c):
> >     if b**2 - 4*a*c < 0:
> >         return 'No real solution.'
> >     else:
> >         sol1 = -1 * b + (sqrt(b**2 - 4*a*c)) / 2*a
> >         sol2 = -1 * b - (sqrt(b**2 - 4*a*c)) / 2*a
> >         return (sol1, sol2)
>
> > # execute
> > print solver(a,b,c)
>
> > Thanks to everyone who helped...
> > This really expanded my knowledge on some of the mathematical
> > functions in Python.
>
> UPDATE:
> '
>
> #import
> from math import sqrt
>
> # collect data
> a = float(raw_input('Type a value: '))
> b = float(raw_input('Type b value: '))
> c = float(raw_input('Type c value: '))
>
> # create solver
> def solver(a,b,c):
>     if b**2 - 4*a*c < 0:
>         return 'No real solution.'
>     else:
>         sol1 = (-1 * b + (sqrt(b**2 - 4*a*c))) / 2*a
>         sol2 = (-1 * b - (sqrt(b**2 - 4*a*c))) / 2*a
>         return (sol1, sol2)
>
> # execute
> print solver(a,b,c)

You need to put your denominator, 2*a, in parens. The way it stands,
you are dividing by 2, then multiplying by a. That's not what you
want.

Also, for better style, I suggest you compute the discriminanat once
and store it for reuse rather than repeating the expression three
times.

```