Factoring Polynomials

James Mills prologic at shortcircuit.net.au
Thu Dec 18 20:26:14 EST 2008


Hi Collin,

Here you go:

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
    x = (-1 * b) + (((b**2 - (4 * a * c)) ** 0.5) / (2 * a))
    return (-1 * x), x

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" % x[0]
        print "x = +%0.2f" % x[1]
jmills at atomant:~/tmp$

cheers
James



More information about the Python-list mailing list