Teaching Kids Programming: Videos on Data Structures and Algorithms
Given a quadratic equation
it has two real roots (which could be the same) if and only if 
Proof of Quadratic Equation Roots

Let’s move c to the other side: 
And multiply both sides with 4a so we get 
And add term
and this becomes 
Transform left side: 
Thus, we have two roots:


As these two roots are symmetric so we can get the symmetric axis for a quadratic equation
is 
Python Function to Compute Real Roots of a Quadratic Equation
We need to check the case when a is zero or the function does not intersect the X-axis (no real roots).
def realRootsForQuadraticEquation(a, b, c):
if a == 0:
raise ValueError("a can't be zero")
if b*b-4*a*c < 0:
raise Exception("No real roots for {}x^2+{}x+{}=0".format(a, b, c))
r = sqrt(b*b-4*a*c)
x1 = (-b + r) / (2 * a)
x2 = (-b - r) / (2 * a)
return (x1, x2)
We are returning the roots as a tuple. The time and space complexity is O(1).
–EOF (The Ultimate Computing & Technology Blog) —
738 wordsLast Post: Teaching Kids Programming - Compute the Maximal Perimeter by Forming a Rectangle from N squares
Next Post: Teaching Kids Programming - Convert 1-D Array to 2D Matrix (Reshape Algorithm)