In logic and set theory, Venn diagram problems involve breaking down a collection of items into overlapping categories.
Instead of computing formulas on a calculator, we can use Prolog — a declarative programming language widely used in Artificial Intelligence — to state facts about the categories and let the computer deduce the answer through logical inference!
Consider a puzzle inspired by Problem #10 from the 1971 MAA High School Mathematics Contest:
A car dealership has 50 cars on its lot. Each car is either an SUV or a Sedan, and each car is either Electric or Gas-powered.
If 31 are SUVs, 18 are electric, and 14 are gas-powered sedans, then how many electric SUVs are on the lot?
Let's represent the given information logically:
Total electric cars = 18 $\implies$ Electric SUVs = $18 - 5 = 13$
In Prolog, we write facts and rules, and then ask Prolog to satisfy a goal.
total_cars(50). sedans(N) :- total_cars(T), suvs(S), N is T - S. electric_suvs(Ans) :- electric_total(Et), electric_sedans(Es), Ans is Et - Es. goal: electric_suvs(Ans).
Move the mouse over a dotted box for more information.
In Prolog, the symbol :- means "if", and uppercase letters (like Ans, T, S) denote variables that Prolog will solve for automatically.
Now you try.
Replace ???? in the three rules with T - S, Sed - Gs, and Et - Es. Run the goal to see Prolog solve for Ans = 13!
Type your code here:
See your results here:
This Prolog code has ???? in each arithmetic rule. Replace the ???? with the appropriate arithmetic expressions: T - S, Sed - Gs, and Et - Es. Then hit Run to let Prolog deduce the number of electric SUVs!
Share your code
Show a friend, family member, or teacher what you've done!