# Factoring Polynomials

Russ P. Russ.Paielli at gmail.com
Fri Dec 19 03:41:57 CET 2008

```On Dec 18, 6:31 pm, Collin D <collin.da... at gmail.com> wrote:
> On Dec 18, 6:27 pm, Collin D <collin.da... at gmail.com> wrote:
>
>
>
> > On Dec 18, 6:23 pm, "Russ P." <Russ.Paie... at gmail.com> wrote:
>
> > > 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.- Hide quoted text -
>
> > > - Show quoted text -
>
> > I see what you mean on the denominator and discriminant. Ill do that.- Hide quoted text -
>
> > - Show quoted text -
>
> 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: '))
>
> # find discriminant
> disc = b**2 - 4*a*c
>
> # create solver
> def solver(a,b,c):
>     if disc < 0:
>         return 'No real solution.'
>     else:
>         sol1 = (-1 * b + (sqrt(disc))) / (2*a)
>         sol2 = (-1 * b - (sqrt(disc))) / (2*a)
>         return (sol1, sol2)
>
> # execute
> print solver(a,b,c)

A couple of style points. I would use -b rather than -1 * b. Also, you
don't need the else clause. You can simplify it to

def solver(a, b, c):

disc = b**2 - 4 * a * c

if disc < 0: return "No real solution."

sol1 = (-b + sqrt(disc)) / (2*a)
sol2 = (-b - sqrt(disc)) / (2*a)

return sol1, sol2

```

More information about the Python-list mailing list