Skip to content

Instantly share code, notes, and snippets.

@SYZYGY-DEV333
Last active February 10, 2016 22:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SYZYGY-DEV333/e63fd88ad860e6b8c401 to your computer and use it in GitHub Desktop.
Save SYZYGY-DEV333/e63fd88ad860e6b8c401 to your computer and use it in GitHub Desktop.
Factor a polynomial using the roots
#!/usr/bin/env python
# Polynomial Root Finder
# SYZYGY-DEV333
# Apache 2.0
step = 0.001 # smaller steps are more accurate, but slower
start = -10 # these parameters should be fine at -10 and 10
stop = 10
print "Python Polynomial Factoring"
print "use ** to mean ^"
print "Format: a*x**j + b*x**k + c (not ax^j+bx^k+c)"
print "examples: x**2 - 2*x - 24 , 3*x**3 + 2*x**2 + x +3"
while 1 == 1:
print
equ = raw_input("0 = ") # asks for your equation
f = lambda x: eval(equ,{"__builtins__":None},{"x":x}) # for saftey, won't accept anything other than an equation
sign = f(start) > 0
z = start
while z <= stop:
value = f(z)
if value == 0:
print "(x +", z, ")"
elif (value > 0) != sign:
print "(x +", z, ")"
sign = value > 0
z += step
@SYZYGY-DEV333
Copy link
Author

Polynomial Factoring using Python

This script is based on my Rootfinder, but factors it instead. I wrote it for my math class because doing it by hand is a pain. So yeah, this works. Saved me a lot of time.

How to install

Run the following commands in your Unix terminal:

wget http://bit.ly/1Pm0Gcs -O polyfactor.py
chmod +x polyfactor.py

Then you can run the program with ./polyfactor.py.
Have fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment