Factoring Polynomials

Collin D collin.day.0 at gmail.com
Fri Dec 19 03:17:19 CET 2008


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)




More information about the Python-list mailing list