Skip to content

Instantly share code, notes, and snippets.

@SYZYGY-DEV333
Last active February 7, 2016 13:40
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/237cd89b014243d026d5 to your computer and use it in GitHub Desktop.
Save SYZYGY-DEV333/237cd89b014243d026d5 to your computer and use it in GitHub Desktop.
Polynomial root finder in Python. Quick and dirty, but it works. Have fun! :-)
#!/usr/bin/env python
# Polynomial Root Finder
# SYZYGY-DEV333
# Apache 2.0
print "Python Polynomial Root Finder"
print "Use '**' to mean '^'"
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
step = 0.001 # smaller steps are more accurate, but slower
start = -10 # these parameters should be fine at -10 and 10
stop = 10
sign = f(start) > 0
z = start
while z <= stop:
value = f(z)
if value == 0:
print "Root found at", z
elif (value > 0) != sign:
print "Root found near", z
sign = value > 0
z += step
@SYZYGY-DEV333
Copy link
Author

Polynomial Root Finder

Simple polynomial root finder made in Python. It's quick and dirty and horribly inefficient, but it works. Just type in the equation when prompted. You have to change it a little though, so -7x^2+8x+10 would become -7 * x ** 2 + 8 * x + 10. That's all, have fun! :-)

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