Lesson goal: Advice on handling a quadratic

Previous: Taking derivatives | Home | Next: Traveling between cities

For having to keep your logic straight, nothing beats a quadratic equation. Do you factor it? Use the quadratic formula? Maybe there's two roots? One root? No real roots?

How about some Prolog code that allows you to enter in a quadratic. It'll look at it and advise you on what to do with.

The underlying logic of "what to do" with a quadratic is to think of the quadratic formula. If you have a quadractic equation in "standard form," which is $Ax^2+Bx+C=0$, then the two solutions to this equation will be $$x=\frac{-B\pm\sqrt{B^2-4AC}}{2A}.$$ Now, if you look at the $B^2-4AC$, you can figure out what to do. In particular:
  • If it's $<0$, the equation won't have any real roots (because of the square-root).
  • If it's $=0$, the equation will have $1$ root, because there won't be anything to add or subtract in the $\pm$ step.
  • If it's a perfect square, then you'll be able to factor the equation.
  • If it's not a perfect square, then you'll have to use the quadratic formula to solve the equation.
We can put this logic into some Prolog code almost exactly following the logic of these steps.
Move the mouse over a dotted box for more information.

Now you try. Run the code as is and see what it does. Try changing the quadratic too.

Type your code here:

See your results here: