One of the coolest things I've seen is to use Prolog with CLP(FD) to solve the 7-11 problem. The problem basically says the sum of the prices of four items is $7.11, and the product is $7.11 too (no rounding); find the prices of these four items. This can be solved in two lines of code that gives the (unique) solution in a second. Not even my expensive Mathematica can do this! ?- use_module(library(clpfd)). true. ?-…
p = 711
q = 711000000
for a in range(1, 1 + p // 4):
if q % a == 0:
for b in range(a, 1 + (p - a) // 3):
if q % (a * b) == 0:
for c in range(b, 1 + (p - a - b) // 2):
d = p - a - b - c
if a * b * c * d == q:
print (a, b, c, d)
Does Prolog do the same pruning? Alternatively, it could be dumber (omit some of the pruning that my code does) or smarter (start by factoring 711000000 or something).