This article is part of in the series

Here's a fun way to use Python: input any three numbers into a function and you can solve the quadratic equation in milliseconds. Bet you wish you knew about this one back when you were in Algebra I class.

The function, written by the people over at Programiz, solves the quadratic equation using basic multiplication and division operations in Python. If you don't remember, to solve the quadratic equation you must take the opposite of b, plus or minus the square root of b squared, minus 4 times a times c over (divided by) 2 times a. In the quadratic equation, a, b, and c can be any integers, positive or negative, as long as a doesn't equal zero. In the example below, a equals 1, b equals 5, and c equals  6, but you can set them to be any numbers you like.

Here is the snippet:

a = 1
b = 5
c = 6

# To take coefficient input from the users
# a = float(input('Enter a: '))
# b = float(input('Enter b: '))
# c = float(input('Enter c: '))

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

As you can see, the snippet finds two possible solutions (as it's meant to), because you're meant to take the opposite of b plus or minus b squared, etc, and this equation accounts for that.

The snippet's output is as follows:

print('The solution are {0} and {1}'.format(sol1,sol2))

Now that you have the formula, add your own numbers to see if the equation works for you.