# Find Roots Of A Parabola

This is used for finding the roots of a parabola. The parabola must have a number in front of x2 (x squared), a number in front of the single x, and a following number, or you can have a perfect square parabola. (in the form "number x2 - number")
This function returns a string.

This code snippet comes with 3 functions.
Find_Roots(parabola) - finds the roots of a parabola
Find_Multi(mult, num) - finds what numbers add together to get "num" and multiply together to get "mult"
Is_SquareRoot(number) - returns True if number is a perfect square

``````import re, string

def Find_Roots(parabola):
"""Finds the roots of a parabola"""
ro = re.compile("[1-9]*x2[-, +][1-9]*x[+, -][1-9]*") # Compiles expressions to check if argument is a parabola
rw = re.compile("[1-9]*x2-[1-9]*")
if ro.match(parabola):
list1 = string.split(parabola, "+")
w = string.join(list1, "-")
list2 = string.split(w, "-", 2)
square = int(list2.replace("x2", ""))
x = int(list2.replace("x", ""))
num = int(list2)
if square == 1:
roots = Find_Multi(num, x)
if abs(roots) == roots:
roots = string.join(["+", str(roots)])
else:
roots = str(roots).replace("-", "- ")
if abs(roots) == roots:
roots = string.join(["+", str(roots)])
else:
roots = str(roots).replace("-", "- ")
x = "(x %s)(x %s)\n(x=%s)(x=%s)" % (roots, roots, -(int(roots)), -(int(roots)))
return x
else:
num *= square
place = [square, square]
roots = Find_Multi(num, x)
r = []
if roots/square == roots/float(square):
roots /= square
place = ""
r.append(roots)
else:
if (-(roots))/square == (-(roots))/float(square):
r.append((-(roots))/square)
else:
r.append("%s/%s" % (-(roots), square))
if roots/square == roots/float(square):
roots /= square
place = ""
r.append(roots)
else:
if (-(roots))/square == (-(roots))/float(square):
r.append((-(roots))/square)
else:
r.append("%s/%s" % (-(roots), square))
if abs(roots) == roots:
roots = string.join(["+", str(roots)])
else:
roots = str(roots).replace("-", "- ")
if abs(roots) == roots:
roots = string.join(["+", str(roots)])
else:
roots = str(roots).replace("-", "- ")
x = "(%sx %s)(%sx %s)\n(x=%s)(x=%s)" % (place, roots, place, roots, r,r)
return x
elif rw.match(parabola):
list1 = string.split(parabola, "-")
square = int((str(list1).replace("x2", "")))
num = int(list1)
if square > 1:
if Is_SquareRoot(square) and Is_SquareRoot(num):
roots = [int(math.sqrt(num)), int(math.sqrt(num))]
front = [int(math.sqrt(square)), int(math.sqrt(square))]
x = "(%sx - %s)(%sx + %s)" % (front, roots, front, roots)
y = []
if (-(roots))/front == (-(roots))/float(front):
y.append("(x=%s)" % ((-(roots))/front))
else:
y.append("(x=%s/%s)" % (-(roots), front))
if (-(roots))/front == (-(roots))/float(front):
y.append("(x=%s)" %((-(roots))/front))
else:
y.append("(x=%s/%s)" % (-(roots), front))
return string.join([x, "\n", string.join(y)], "")
else:
if Is_SquareRoot(num):
roots = [int(math.sqrt(num)), int(math.sqrt(num))]
x = "(x - %s)(x + %s)" % (roots, roots)
y = "(x = %s)(x = %s)" % (-roots, -roots)
return string.join([x, "\n", y], "")
if type(mult) ==  int:
list1=[]
if -mult > mult:
for i in range(mult, -(mult+1)):
if i:
x = mult/i
y = mult/float(i)
if x == y:
list1.append(i)
else:
for i in range(-mult, mult+1):
if= i:
x = mult/i
y = mult/float(i)
if x == y:
list1.append(i)
x = list1
for i in x:
for w in x:
if i+w == add and i*w == mult:
num = [w, i]
break
return num

def Is_SquareRoot(number):
x = math.sqrt(number)
y = round(x)
if x == y:
return True
else:
return False

if __name__ == "__main__":
print Find_Roots("1x2-9")``````

I only started learning python last week and since I am in Grade 11, I thought it would be a good idea to program something that gives the roots and vertex of any given parabola in the general form. But my code looks a lot simpler and shorter. Why? Btw I couldn't compile your code. It gave errors. O: Confused. Thanks.

My code:

``````# Quadratic Solver
# 10/09/10

from math import exp, sqrt

print "10/09/10"

print \
"""

ax^2+bx+c
__________
x= -b (+-) \/b^2 - 4ac
_____________________
2a

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

bsquared = b**2
four_ac = -4 * a * c
delta = bsquared + four_ac

if delta < 0:
print "\nNo Zeros"
else:
roots = sqrt(delta)

top_pos = -1 * b + roots
top_neg = -1 * b - roots

bottom = 2 * a
root_pos = top_pos / bottom
root_neg = top_neg / bottom

print "\nRoot 1: ", root_pos
print "Root 2: ", root_neg

vertex_top = -1 *b
bottom = 2 * a
h = vertex_top / bottom

vertex_squared = h**2
vertex_1 = a * vertex_squared
vertex_2 = b * h
k = vertex_1 + vertex_2 + c

print "\nVertex: (", h, ",", k, ")"