Factoring Polynomials

James Mills prologic at shortcircuit.net.au
Thu Dec 18 20:30:33 EST 2008


UPDATE:

jmills at atomant:~/tmp$ cat polycalc.py
#!/usr/bin/env python

from math import sqrt

def f(a, b, c):
    if (b**2 - (4 * a * c)) < 0:
        return None, None # Can't solve
    x1 = -b - (sqrt(b**2 - (4 * a * c)) / (2 * a))
    x2 = -b + (sqrt(b**2 - (4 * a * c)) / (2 * a))
    return x1, x2

print "Polynomial Solver..."
print

while True:
    a = float(raw_input("a: "))
    b = float(raw_input("b: "))
    c = float(raw_input("c: "))

    x = f(a, b, c)
    if None in x:
        print "Can't solve!"
    else:
        print "x = (%0.2f, %0.2f)" % x
jmills at atomant:~/tmp$ ./polycalc.py
Polynomial Solver...

a: 1
b: 8
c: 5
x = (-11.32, -4.68)



More information about the Python-list mailing list