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
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:
Put in some quadratic equations from your last math book. Look in the section that gives a variety of equations, asking you how you'd solve (or factor) each.
What do you think the last 3 roots(...) clauses do? Hint: They help the overall code be more "understanding" of what inputs it'll accept.
Share your code
Show a friend, family member, or teacher what you've done!