Teaching Kids Programming – Finding Real Roots of a Quadratic Equation


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given a quadratic equation tex_d16ae3d3d796536d7d7f311555068bcb Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video it has two real roots (which could be the same) if and only if tex_ff8359dc3f77dd369569786e6b316ba5 Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video

Proof of Quadratic Equation Roots

tex_d16ae3d3d796536d7d7f311555068bcb Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video
Let’s move c to the other side: tex_07eb1a9d757a30ccca877366a9ae699d Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video
And multiply both sides with 4a so we get tex_a876efa15909f504100bca78e2d25712 Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video
And add term tex_c09b485d3e83cfa990ebc76bbe769b0e Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video and this becomes tex_6af633eeb4f58527f5c5f6455bf57035 Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video
Transform left side: tex_7bd698f969c6999ce9e5efddc4beba91 Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video
Thus, we have two roots:
tex_a446cf188a5ed58c2ad1eafbc233cd0f Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video
tex_155250201ac836cc17c119cfe9ef6513 Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video

As these two roots are symmetric so we can get the symmetric axis for a quadratic equation tex_d16ae3d3d796536d7d7f311555068bcb Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video is tex_869878ab4bc269707a3bc929e11a265b Teaching Kids Programming - Finding Real Roots of a Quadratic Equation math python teaching kids programming youtube video

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).

1
2
3
4
5
6
7
8
9
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)
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) —

GD Star Rating
loading...
755 words
Last 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)

The Permanent URL is: Teaching Kids Programming – Finding Real Roots of a Quadratic Equation

Leave a Reply